FastJson解析對象及對象數組--項目經驗

原文地址爲:FastJson解析對象及對象數組--項目經驗

第一次使用json,解析工具爲FastJson,使用語言爲java

常見的json解析實例,以map爲例:

Map<String,String> map=new HashMap<String,String>();
map.put(
"code","0");
map.put(
"message","ok");
String json
=JSON.toJSONString(map);
System.out.println(json);
String code
=JSONObject.fromObject(json).getString("code");
String message
=JSONObject.fromObject(json).getString("message");
System.out.println(
"code="+code+",message"+message);

1. Json解析出字符串

 String  rootnode = JSONObject.fromObject(jsonData).from("attribute");

 String childnode= JSONObject.fromObject(rootnode).from("attribute");

 以此類推

 

2. Json解析出單個對象

    JSON.parseObject(jsonString,user.class);

 

3.Json解析出多個對象

  List<user> list=new ArrayList<user>(JSONArray.parseArray(jsonString,user.class));


轉載請註明本文地址:FastJson解析對象及對象數組--項目經驗
發佈了0 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章