fastjson 是一个性能极好的用 Java 语言实现的 JSON 解析器和生成器,来自阿里巴巴的工程师开发。
主要特点:
- 快速FAST (比其它任何基于Java的解析器和生成器更快,包括jackson)
- 强大(支持普通JDK类包括任意Java Bean Class、Collection、Map、Date或enum)
- 零依赖(没有依赖其它任何类库除了JDK)
一 、生成Json:
JavaBean、List<JavaBean>、List<String>、List<Map<String,Object>>
1. String jsonString = JSON.toJSONString(obj);
二、解析Json:
(1)JavaBean
1. Class class= JSON.parseObject(jsonString, Class.class);
(2)List<JavaBean>
1. List<Class> class=JSON.parseArray((jsonString, Class.class);
(3)List<String>
1. List<String> listString = JSON.parseArray(jsonString, String.class);
(4)List<
Map<String,Object>
>
1. List<Map<String, Object>> listMap = JSON.parseObject(jsonString, new TypeReference<List<Map<String,Object>>>(){});
给服务端发送请求后,服务端会返回一连串的数据,这些数据在大部分情况下都是XML格式或者JSON格式。然后JSON相对XML来说解析相对方便一些,所以先说说JSON的解析。
JSON的基本数据格式有这几种:
1.一个JSON对象——JSONObject
{"name":"胡小威" , "age":20 , "male":true}
2.一个JSON数组——JSONArray
[{"name":"胡小威" , "age":20 , "male":true},{"name":"赵小亮" , "age":22 , "male":false}]
3.复杂一点的JSONObject
{"name":"胡小威", "age"=20, "male":true, "address":{"street":"岳麓山南", "city":"长沙","country":"中国"}}
4.复杂一点的JSONArray
[
{"name":"胡小威", "age"=20, "male":true, "address":{"street":"岳麓山南", "city":"长沙","country":"中国"}},
{"name":"赵小亮", "age"=22, "male":false, "address":{"street":"九州港", "city":"珠海","country":"中国"}}
]
5.来个更复杂的,这是我从爱帮公交上扒取的数据
{"buses":{"bus":[{"last_foot_dist":"0","time":"37","segments":{"segment":[{"line_name":"立珊专线(中南大学学生公寓-长沙火车站)","foot_dist":"362","stat_xys":"","stats":"岳麓山南;湖南师大;二里半;岳麓山北;市四医院;华图教育(太平街口);牛耳教育(南阳街口);韭菜园;曙光路口;长岛路口;长沙火车站","end_stat":"长沙火车站","line_xys":"","line_dist":"7535","start_stat":"岳麓山南"}]},"foot_dist":"362","dist":"7897"},{"last_foot_dist":"0","time":"41","segments":{"segment":[{"line_name":"旅1路(科教新村-长沙火车站)","foot_dist":"337","stat_xys":"","stats":"岳麓山南;湖南师大;二里半;岳麓山北;市四医院;华图教育(太平街口);蔡锷中路口;韭菜园;乔庄;曙光路口;长岛路口;五一东村;车站路口;长沙火车站","end_stat":"长沙火车站","line_xys":"","line_dist":"7822","start_stat":"岳麓山南"}]},"foot_dist":"337","dist":"8159"}]}}
接下来是解析部分
一、直接解析
1.{"name":"胡小威" , "age":20 , "male":true}
1. public void
2. try
3. new
4. "name");
5. int age = jsonObject.getInt("age");
6. boolean male = jsonObject.getBoolean("male");
7. //接下来该干嘛干嘛
8. catch
9. // TODO Auto-generated catch block
10. e.printStackTrace();
11. }
12. }
2.[{"name":"胡小威" , "age":20 , "male":true},{"name":"赵小亮" , "age":22 , "male":false}]
1. public void
2. try
3. new
4. for (int i = 0; i < jsonArray.length(); i++) {
5. jsonObject = jsonArray.getJSONObject(i);
6. "name");
7. int age = jsonObject.getInt("age");
8. boolean male = jsonObject.getBoolean("male");
9. //接下来该干嘛干嘛
10. }
11. catch
12. // TODO Auto-generated catch block
13. e.printStackTrace();
14. }
15. }
3.{"name":"胡小威", "age"=20, "male":true, "address":{"street":"岳麓山南", "city":"长沙","country":"中国"}}
1. public void
2. try
3. new
4. "name");
5. int age = jsonObject.getInt("age");
6. boolean male = jsonObject.getBoolean("male");
7. "address");
8. "street");
9. "city");
10. "country");
11. new
12. new
13. catch
14. // TODO Auto-generated catch block
15. e.printStackTrace();
16. }
17. }
4.
[
{"name":"胡小威", "age"=20, "male":true, "address":{"street":"岳麓山南", "city":"长沙","country":"中国"}},
{"name":"赵小亮", "age"=22, "male":false, "address":{"street":"九州港", "city":"珠海","country":"中国"}}
]
1. public void
2. new
3. try
4. new
5. for (int i = 0; i < jsonArray.length(); i++) {
6. jsonObject = jsonArray.getJSONObject(i);
7. "name");
8. int age = jsonObject.getInt("age");
9. boolean male = jsonObject.getBoolean("male");
10. "address");
11. "street");
12. "city");
13. "country");
14. new
15. new
16. persons.add(person);
17. "juno", person.toString());
18. }
19. catch
20. // TODO Auto-generated catch block
21. e.printStackTrace();
22. }
23. "juno", persons.toString());
24. }
5. {"buses":{"bus":[{"last_foot_dist":"0","time":"37","segments":{"segment":[{"line_name":"立珊专线(中南大学学生公寓-长沙火车站)","foot_dist":"362","stat_xys":"","stats":"岳麓山南;湖南师大;二里半;岳麓山北;市四医院;华图教育(太平街口);牛耳教育(南阳街口);韭菜园;曙光路口;长岛路口;长沙火车站","end_stat":"长沙火车站","line_xys":"","line_dist":"7535","start_stat":"岳麓山南"}]},"foot_dist":"362","dist":"7897"},{"last_foot_dist":"0","time":"41","segments":{"segment":[{"line_name":"旅1路(科教新村-长沙火车站)","foot_dist":"337","stat_xys":"","stats":"岳麓山南;湖南师大;二里半;岳麓山北;市四医院;华图教育(太平街口);蔡锷中路口;韭菜园;乔庄;曙光路口;长岛路口;五一东村;车站路口;长沙火车站","end_stat":"长沙火车站","line_xys":"","line_dist":"7822","start_stat":"岳麓山南"}]},"foot_dist":"337","dist":"8159"}]}}
1. public
2. "返回的JSON数据:", str);
3. new
4.
5. try
6. new
7. "buses");
8. "bus");
9. for(int i=0;i<busJSArray.length();i++){
10. JSONObject busJSObject = busJSArray.getJSONObject(i);
11. "segments");
12. "segment");
13. new
14.
15. for(int j=0;j<segJSArray.length();j++){
16. if(j==0){
17. JSONObject segJSObject = segJSArray.getJSONObject(j);
18. "line_name"));
19. "line_name", segJSObject.getString("line_name"));
20. "stats"));
21. "end_stat"));
22. "start_stat"));
23. }
24. if(j==1){
25. JSONObject segJSObject = segJSArray.getJSONObject(j);
26. "line_name"));
27. "line_name", segJSObject.getString("line_name"));
28. "stats"));
29. "end_stat"));
30. "start_stat"));
31. }
32. }
33.
34. busList.add(st);
35. }
36. return
37. catch
38. // TODO Auto-generated catch block
39. e.printStackTrace();
40. }
41. return null;
42. }
总结三点:
1.把对象(如string,map,list,javabean等)转化成json字符串用 toJsonString()
2.把json字符串转成json对象,用new jsonObject()和new jsonArrayObjec()
3.把json字符串转成javabean对象,用parseObject()和parseArray()