Class JSON

  • All Implemented Interfaces:
    JSONAware, JSONStreamAware
    Direct Known Subclasses:
    JSONArray, JSONObject

    public abstract class JSON
    extends Object
    implements JSONStreamAware, JSONAware
    This is the main class for using Fastjson. You usually call these two methods toJSONString(Object) and parseObject(String, Class).

    Here is an example of how fastjson is used for a simple Class:

     Model model = new Model();
     String json = JSON.toJSONString(model); // serializes model to Json
     Model model2 = JSON.parseObject(json, Model.class); // deserializes json into model2
     

    If the object that your are serializing/deserializing is a ParameterizedType (i.e. contains at least one type parameter and may be an array) then you must use the toJSONString(Object) or parseObject(String, Type, Feature[]) method. Here is an example for serializing and deserialing a ParameterizedType:

     String json = "[{},...]";
     Type listType = new TypeReference<List<Model>>() {}.getType();
     List<Model> modelList = JSON.parseObject(json, listType);
     
    Author:
    wenshao[szujobs@hotmail.com]
    See Also:
    TypeReference