FastJSON 常用操作

下载地址:https://github.com/alibaba/fastjson

Fastjson API入口类是com.alibaba.fastjson.JSON,常用的序列化操作都可以在JSON类上的静态方法直接完成。常用api如下:

public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray 
public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject    
public static final <T> T parseObject(String text,Class<T> clazz); // 把JSON文本parse为JavaBean 
public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray 
public static final <T> List<T> parseArray(String text,Class<T> clazz); //把JSON文本parse成JavaBean集合 
public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本 
public static final String toJSONString(Object object,boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本 
public static final Object toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。

还有一些其他类库:

SerializeWriter:相当于StringBuffer
JSONArray:相当于List<Object>
JSONObject:相当于Map<String,Object>


1、JSON测试:

public class Test { 
  private static SerializeConfig mapping = new SerializeConfig(); static { 
    mapping.put(Date.class,new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss")); 
  } 

  public static void main(String[] args) { 
    Foo f1 = new Foo(); 
    Date date = new Date(); 
    String text = JSON.toJSONString(date,mapping); 
    System.out.println(text); 
    System.out.println(JSON.toJSONString(f1,true)); 
    String x2 =JSON.toJSONString(f1,mapping); 
    System.out.println(x2); 
  } 

  public static void json2List(){ 
    //List -> JSON array 
    List<Bar> barList = new ArrayList<Bar>(); 
    barList.add(new Bar()); 
    barList.add(new Bar()); 
    barList.add(new Bar()); 
    String json= JSON.toJSONString(barList,true); 
    System.out.println(json); 
    //JSON array -> List 
    List<Bar> barList1 = JSON.parseArray(json,Bar.class); 
    for (Bar bar : barList1) { 
      System.out.println(bar.toString()); 
    } 
  } 

  public static void json2Map(){ 
    //Map -> JSON 
    Map<String,Bar> map = new HashMap<String,Bar>(); 
    map.put("a",new Bar()); 
    map.put("b",new Bar()); 
    map.put("c",new Bar()); 
    String json = JSON.toJSONString(map,true); 
    System.out.println(json); 
    //JSON -> Map 
    Map<String,Bar> map1 = (Map<String,Bar>)JSON.parse(json); 
    for (String key : map1.keySet()) { 
      System.out.println(key+":"+map1.get(key)); 
    } 
  } 

  public static void array2JSON(){ 
    String[] arr_String    = {"a","b","c"}; 
    String json_arr_String = JSON.toJSONString(arr_String,true); 
    System.out.println(json_arr_String); 
    JSONArray jsonArray = JSON.parseArray(json_arr_String); 
    for (Object o : jsonArray) { 
      System.out.println(o); 
    } 
    System.out.println(jsonArray); 
  } 
  public static void array2JSON2(){ 
    Bar[] arr_Bar    = {new Bar(),new Bar(),new Bar()}; 
    String json_arr_Bar = JSON.toJSONString(arr_Bar,true); 
    System.out.println(json_arr_Bar); 
    JSONArray jsonArray = JSON.parseArray(json_arr_Bar); 
    for (Object o : jsonArray) { 
      System.out.println(o); 
    } 
    System.out.println(jsonArray); 
  } 

  public static void map2JSON(){ 
    Map map = new HashMap(); 
    map.put("a","aaa"); 
    map.put("b","bbb"); 
    map.put("c","ccc"); 
    String json=JSON.toJSONString(map); 
    System.out.println(json); 
    Map map1 = JSON.parseObject(json); 
    for (Object o : map.entrySet()) { 
      Map.Entry<String,String> entry = (Map.Entry<String,String>)o; 
      System.out.println(entry.getKey()+"--->"+entry.getValue()); 
    } 
  } 
}


2、也可以使用JSONArray、JSONObject。例如:

String str = "...";
List<Bar> list = JSONArray.parseArray(itemJson,Bar.class);
JSONArray arr = JSONArray.parseArray(str);

String str1 = "...";
Bar bar = JSONObject.parseObject(str1,Bar.class);
JSONObject obj = JSONObject.parseObject(str1);

3、其他:

在做JSON反序列化的时候,我们可能经常传递一个class对象来获取对象的示例。但有的时候,可能并不存在这样的class对象。这时可以不定义任何类,直接通过JSONArray和JSONObject来访问。

1)JSONObject用来存放非内置对象,组织形式是一个键值对。事实上,JSONObject是一个Map。解析成功后,如果是Java内置对象,可以直接调用相应方法获取值,比如ublic Integer getInteger(String key)。如果不是内置对象,可以调用public JSONObject getJSONObject(String key)来读取对象。如果还是数组,可以调用public JSONArray getJSONArray(String key)。

2)JSONArray用来存放JSON数组。解析成功后,如果是Java内置对象,可以直接调用相应方法获取值,比如public Integer getInteger(int index)。如果不是内置对象,可以调用public JSONObject getJSONObject(int index)来读取对象。如果还是数组,可以调用public JSONArray getJSONArray(int index)。事实上,JSONArray就是一个List。

补充:JSONObject有containsKey()、getString()等方法

参考:http://www.tuicool.com/articles/zUbQfa

相关文章

文章浏览阅读2.4k次。最近要优化cesium里的热力图效果,浏览...
文章浏览阅读1.2w次,点赞3次,收藏19次。在 Python中读取 j...
文章浏览阅读1.4k次。首字母缩略词 API 代表应用程序编程接口...
文章浏览阅读802次,点赞10次,收藏10次。解决一个JSON反序列...
文章浏览阅读882次。Unity Json和Xml的序列化和反序列化_uni...