导入fastjson依赖的jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>
先看看数据结构:
对应json字符串:
{
"item":[
{
"media_id":"DjkC7IK8dPuF6LkHXdpM7bGRF_ae0dk0ZG_0XIi-rvo",
"content":{
"news_item":[
{
"title":"1",
"author":"欧阳宇",
"digest":"1",
"content":"neirong",
"content_source_url":"http://ouyangyu.com",
"thumb_media_id":"DjkC7IK8dPuF6LkHXdpM7S6ApFtUuIYUXJ9bgr4KTuA",
"show_cover_pic":0,
"url":"url",
"thumb_url":"url",
"need_open_comment":1,
"only_fans_can_comment":0
},
{
"title":"1",
"author":"欧阳宇",
"digest":"1",
"content":"neirong",
"content_source_url":"http://ouyangyu.com",
"thumb_media_id":"DjkC7IK8dPuF6LkHXdpM7S6ApFtUuIYUXJ9bgr4KTuA",
"show_cover_pic":0,
"url":"url",
"thumb_url":"url",
"need_open_comment":1,
"only_fans_can_comment":0
}
],
"create_time":1526465140,
"update_time":1526465513
},
"update_time":1526465513
},
{
"media_id":"DjkC7IK8dPuF6LkHXdpM7T_W4zTBQZtLsxEzFW1c2iE",
"content":{
"news_item":[
{
"title":"1",
"author":"欧阳宇",
"digest":"1",
"content":"neirong",
"content_source_url":"http://ouyangyu.com",
"thumb_media_id":"DjkC7IK8dPuF6LkHXdpM7S6ApFtUuIYUXJ9bgr4KTuA",
"show_cover_pic":0,
"url":"url",
"thumb_url":"url",
"need_open_comment":1,
"only_fans_can_comment":0
}
],
"create_time":1526465140,
"update_time":1526465513
},
"update_time":1526465513
}
],
"total_count":4,
"item_count":4
}
解析代码:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class Test3 {
public static void main(String[] args) throws Exception {
String jsonStr = "{\"item\":[" +
"{\"media_id\":\"DjkC7IK8dPuF6LkHXdpM7bGRF_ae0dk0ZG_0XIi-rvo\","+
"\"content\":{\"news_item\":["+
"{\"title\":\"1\",\"author\":\"欧阳宇\",\"digest\":\"1\","+
"\"content\":\"neirong\","+
"\"content_source_url\":\"http://ouyangyu.com\","+
"\"thumb_media_id\":\"DjkC7IK8dPuF6LkHXdpM7S6ApFtUuIYUXJ9bgr4KTuA\","+
"\"show_cover_pic\":0,"+
"\"url\":\"url\","+
"\"thumb_url\":\"url\","+
"\"need_open_comment\":1,"+
"\"only_fans_can_comment\":0"+
"},"+
"{\"title\":\"1\",\"author\":\"欧阳宇\",\"digest\":\"1\","+
"\"content\":\"neirong\","+
"\"content_source_url\":\"http://ouyangyu.com\","+
"\"thumb_media_id\":\"DjkC7IK8dPuF6LkHXdpM7S6ApFtUuIYUXJ9bgr4KTuA\","+
"\"show_cover_pic\":0,"+
"\"url\":\"url\","+
"\"thumb_url\":\"url\","+
"\"need_open_comment\":1,"+
"\"only_fans_can_comment\":0"+
"}"+
"],"+
"\"create_time\":1526465140,"+
"\"update_time\":1526465513"+
"},"+
"\"update_time\":1526465513"+
"},"+
"{\"media_id\":\"DjkC7IK8dPuF6LkHXdpM7T_W4zTBQZtLsxEzFW1c2iE\","+
"\"content\":{\"news_item\":["+
"{\"title\":\"1\",\"author\":\"欧阳宇\",\"digest\":\"1\","+
"\"content\":\"neirong\","+
"\"content_source_url\":\"http://ouyangyu.com\","+
"\"thumb_media_id\":\"DjkC7IK8dPuF6LkHXdpM7S6ApFtUuIYUXJ9bgr4KTuA\","+
"\"show_cover_pic\":0,"+
"\"url\":\"url\","+
"\"thumb_url\":\"url\","+
"\"need_open_comment\":1,"+
"\"only_fans_can_comment\":0"+
"}"+
"],"+
"\"create_time\":1526465140,"+
"\"update_time\":1526465513"+
"},"+
"\"update_time\":1526465513"+
"}"+
"],"+
"\"total_count\":4," +
"\"item_count\":4"+
"}";
String jsonStr2 = "{\"list\":["+
"{\"ref_date\":\"2018-05-16\",\"user_source\":0,\"msgid\":\"2455330874_1\",\"title\":\"fdsa\","+
"\"int_page_read_user\":1,\"int_page_read_count\":1,\"ori_page_read_user\":0,\"ori_page_read_count\":0,"+
"\"share_user\":0,\"share_count\":0,\"add_to_fav_user\":0,\"add_to_fav_count\":0},"+
"{\"ref_date\":\"2018-05-16\",\"user_source\":0,\"msgid\":\"2455330883_1\",\"title\":\"1\","+
"\"int_page_read_user\":1,\"int_page_read_count\":2,\"ori_page_read_user\":1,\"ori_page_read_count\":1,"+
"\"share_user\":0,\"share_count\":0,\"add_to_fav_user\":0,\"add_to_fav_count\":0},"+
"{\"ref_date\":\"2018-05-16\",\"user_source\":0,\"msgid\":\"2455330883_2\",\"title\":\"2\","+
"\"int_page_read_user\":1,\"int_page_read_count\":1,\"ori_page_read_user\":1,\"ori_page_read_count\":1,"+
"\"share_user\":0,\"share_count\":0,\"add_to_fav_user\":0,\"add_to_fav_count\":0},"+
"{\"ref_date\":\"2018-05-16\",\"user_source\":0,\"msgid\":\"2455330883_3\",\"title\":\"3\",\"int_page_read_user\":1,\"int_page_read_count\":1,\"ori_page_read_user\":0,\"ori_page_read_count\":0,\"share_user\":0,\"share_count\":0,\"add_to_fav_user\":0,\"add_to_fav_count\":0},"+
"{\"ref_date\":\"2018-05-16\",\"user_source\":0,\"msgid\":\"2455330883_4\",\"title\":\"4\",\"int_page_read_user\":1,\"int_page_read_count\":1,\"ori_page_read_user\":0,\"ori_page_read_count\":0,\"share_user\":0,\"share_count\":0,\"add_to_fav_user\":0,\"add_to_fav_count\":0},"+
"{\"ref_date\":\"2018-05-16\",\"user_source\":0,\"msgid\":\"2455330883_5\",\"title\":\"5\",\"int_page_read_user\":1,\"int_page_read_count\":1,\"ori_page_read_user\":0,\"ori_page_read_count\":0,\"share_user\":0,\"share_count\":0,\"add_to_fav_user\":0,\"add_to_fav_count\":0}"+
"]"+
"}";
//原json字符串
System.out.println("原json字符串");
System.out.println("jsonStr:"+jsonStr);
//1.第一层解析
JSONObject map1 = JSON.parseObject(jsonStr);
JSONArray item = map1.getJSONArray("item");
Integer total_count = (Integer)map1.get("total_count");
Integer item_count = (Integer)map1.get("item_count");
System.out.println("第一层解析");
System.out.println("item:"+item.toJSONString());
System.out.println("total_count:"+total_count);
System.out.println("item_count:"+item_count);
//2.第二层解析
for(int i=0;i<item.size();i++){
JSONObject map2 = item.getJSONObject(i);
Integer update_time = (Integer) map2.get("update_time");
JSONObject content = map2.getJSONObject("content");
JSONArray news_item = content.getJSONArray("news_item");
Integer create_time = (Integer) content.get("create_time");
Integer update_time2 = (Integer) content.get("update_time");
System.out.println("第二层解析");
System.out.println("content:"+content.toJSONString());
System.out.println("create_time:"+create_time);
System.out.println("update_time2:"+update_time2);
//3.第三层解析
for(int j=0;j<news_item.size();j++){
JSONObject map3 = news_item.getJSONObject(j);
String title = (String) map3.get("title");
String author = (String) map3.get("author");
String content_source_url = (String) map3.get("content_source_url");
Integer need_open_comment = (Integer) map3.get("need_open_comment");
System.out.println("第三层解析");
System.out.println("title:"+title);
System.out.println("author:"+author);
System.out.println("content_source_url:"+content_source_url);
System.out.println("need_open_comment:"+need_open_comment);
}
}
}
}
结果:大致如图
结论:主要使用两个对象的方法
JSONObject map1 = JSON.parseObject(jsonStr); //map对象
JSONArray item = map1.getJSONArray("item"); //数组对象
注意:map获值的类型:字符串或者基本数据类型,注意数据类型
方法2:gons解析
Gson g1=new Gson();
Map m3 = g1.fromJson(jsonStr, HashMap.class);
System.out.println(m3.toString());
System.out.println(m3.get("item_count"));
System.out.println(m3.get("item"));
ArrayList l1=(ArrayList) m3.get("item");
for(int i=0;i<l1.size();i++){
Map m4 = (Map)l1.get(i);
System.out.println("update_time:"+m4.get("update_time"));
}