json解析2 - 复杂json解析

电玩女神 2022-12-10 15:00 381阅读 0赞

导入fastjson依赖的jar包

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>fastjson</artifactId>
  4. <version>1.2.60</version>
  5. </dependency>

先看看数据结构:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lpZ3VhbmdfODIw_size_16_color_FFFFFF_t_70

对应json字符串:

  1. {
  2. "item":[
  3. {
  4. "media_id":"DjkC7IK8dPuF6LkHXdpM7bGRF_ae0dk0ZG_0XIi-rvo",
  5. "content":{
  6. "news_item":[
  7. {
  8. "title":"1",
  9. "author":"欧阳宇",
  10. "digest":"1",
  11. "content":"neirong",
  12. "content_source_url":"http://ouyangyu.com",
  13. "thumb_media_id":"DjkC7IK8dPuF6LkHXdpM7S6ApFtUuIYUXJ9bgr4KTuA",
  14. "show_cover_pic":0,
  15. "url":"url",
  16. "thumb_url":"url",
  17. "need_open_comment":1,
  18. "only_fans_can_comment":0
  19. },
  20. {
  21. "title":"1",
  22. "author":"欧阳宇",
  23. "digest":"1",
  24. "content":"neirong",
  25. "content_source_url":"http://ouyangyu.com",
  26. "thumb_media_id":"DjkC7IK8dPuF6LkHXdpM7S6ApFtUuIYUXJ9bgr4KTuA",
  27. "show_cover_pic":0,
  28. "url":"url",
  29. "thumb_url":"url",
  30. "need_open_comment":1,
  31. "only_fans_can_comment":0
  32. }
  33. ],
  34. "create_time":1526465140,
  35. "update_time":1526465513
  36. },
  37. "update_time":1526465513
  38. },
  39. {
  40. "media_id":"DjkC7IK8dPuF6LkHXdpM7T_W4zTBQZtLsxEzFW1c2iE",
  41. "content":{
  42. "news_item":[
  43. {
  44. "title":"1",
  45. "author":"欧阳宇",
  46. "digest":"1",
  47. "content":"neirong",
  48. "content_source_url":"http://ouyangyu.com",
  49. "thumb_media_id":"DjkC7IK8dPuF6LkHXdpM7S6ApFtUuIYUXJ9bgr4KTuA",
  50. "show_cover_pic":0,
  51. "url":"url",
  52. "thumb_url":"url",
  53. "need_open_comment":1,
  54. "only_fans_can_comment":0
  55. }
  56. ],
  57. "create_time":1526465140,
  58. "update_time":1526465513
  59. },
  60. "update_time":1526465513
  61. }
  62. ],
  63. "total_count":4,
  64. "item_count":4
  65. }

解析代码:

  1. import com.alibaba.fastjson.JSON;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. public class Test3 {
  5. public static void main(String[] args) throws Exception {
  6. String jsonStr = "{\"item\":[" +
  7. "{\"media_id\":\"DjkC7IK8dPuF6LkHXdpM7bGRF_ae0dk0ZG_0XIi-rvo\","+
  8. "\"content\":{\"news_item\":["+
  9. "{\"title\":\"1\",\"author\":\"欧阳宇\",\"digest\":\"1\","+
  10. "\"content\":\"neirong\","+
  11. "\"content_source_url\":\"http://ouyangyu.com\","+
  12. "\"thumb_media_id\":\"DjkC7IK8dPuF6LkHXdpM7S6ApFtUuIYUXJ9bgr4KTuA\","+
  13. "\"show_cover_pic\":0,"+
  14. "\"url\":\"url\","+
  15. "\"thumb_url\":\"url\","+
  16. "\"need_open_comment\":1,"+
  17. "\"only_fans_can_comment\":0"+
  18. "},"+
  19. "{\"title\":\"1\",\"author\":\"欧阳宇\",\"digest\":\"1\","+
  20. "\"content\":\"neirong\","+
  21. "\"content_source_url\":\"http://ouyangyu.com\","+
  22. "\"thumb_media_id\":\"DjkC7IK8dPuF6LkHXdpM7S6ApFtUuIYUXJ9bgr4KTuA\","+
  23. "\"show_cover_pic\":0,"+
  24. "\"url\":\"url\","+
  25. "\"thumb_url\":\"url\","+
  26. "\"need_open_comment\":1,"+
  27. "\"only_fans_can_comment\":0"+
  28. "}"+
  29. "],"+
  30. "\"create_time\":1526465140,"+
  31. "\"update_time\":1526465513"+
  32. "},"+
  33. "\"update_time\":1526465513"+
  34. "},"+
  35. "{\"media_id\":\"DjkC7IK8dPuF6LkHXdpM7T_W4zTBQZtLsxEzFW1c2iE\","+
  36. "\"content\":{\"news_item\":["+
  37. "{\"title\":\"1\",\"author\":\"欧阳宇\",\"digest\":\"1\","+
  38. "\"content\":\"neirong\","+
  39. "\"content_source_url\":\"http://ouyangyu.com\","+
  40. "\"thumb_media_id\":\"DjkC7IK8dPuF6LkHXdpM7S6ApFtUuIYUXJ9bgr4KTuA\","+
  41. "\"show_cover_pic\":0,"+
  42. "\"url\":\"url\","+
  43. "\"thumb_url\":\"url\","+
  44. "\"need_open_comment\":1,"+
  45. "\"only_fans_can_comment\":0"+
  46. "}"+
  47. "],"+
  48. "\"create_time\":1526465140,"+
  49. "\"update_time\":1526465513"+
  50. "},"+
  51. "\"update_time\":1526465513"+
  52. "}"+
  53. "],"+
  54. "\"total_count\":4," +
  55. "\"item_count\":4"+
  56. "}";
  57. String jsonStr2 = "{\"list\":["+
  58. "{\"ref_date\":\"2018-05-16\",\"user_source\":0,\"msgid\":\"2455330874_1\",\"title\":\"fdsa\","+
  59. "\"int_page_read_user\":1,\"int_page_read_count\":1,\"ori_page_read_user\":0,\"ori_page_read_count\":0,"+
  60. "\"share_user\":0,\"share_count\":0,\"add_to_fav_user\":0,\"add_to_fav_count\":0},"+
  61. "{\"ref_date\":\"2018-05-16\",\"user_source\":0,\"msgid\":\"2455330883_1\",\"title\":\"1\","+
  62. "\"int_page_read_user\":1,\"int_page_read_count\":2,\"ori_page_read_user\":1,\"ori_page_read_count\":1,"+
  63. "\"share_user\":0,\"share_count\":0,\"add_to_fav_user\":0,\"add_to_fav_count\":0},"+
  64. "{\"ref_date\":\"2018-05-16\",\"user_source\":0,\"msgid\":\"2455330883_2\",\"title\":\"2\","+
  65. "\"int_page_read_user\":1,\"int_page_read_count\":1,\"ori_page_read_user\":1,\"ori_page_read_count\":1,"+
  66. "\"share_user\":0,\"share_count\":0,\"add_to_fav_user\":0,\"add_to_fav_count\":0},"+
  67. "{\"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},"+
  68. "{\"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},"+
  69. "{\"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}"+
  70. "]"+
  71. "}";
  72. //原json字符串
  73. System.out.println("原json字符串");
  74. System.out.println("jsonStr:"+jsonStr);
  75. //1.第一层解析
  76. JSONObject map1 = JSON.parseObject(jsonStr);
  77. JSONArray item = map1.getJSONArray("item");
  78. Integer total_count = (Integer)map1.get("total_count");
  79. Integer item_count = (Integer)map1.get("item_count");
  80. System.out.println("第一层解析");
  81. System.out.println("item:"+item.toJSONString());
  82. System.out.println("total_count:"+total_count);
  83. System.out.println("item_count:"+item_count);
  84. //2.第二层解析
  85. for(int i=0;i<item.size();i++){
  86. JSONObject map2 = item.getJSONObject(i);
  87. Integer update_time = (Integer) map2.get("update_time");
  88. JSONObject content = map2.getJSONObject("content");
  89. JSONArray news_item = content.getJSONArray("news_item");
  90. Integer create_time = (Integer) content.get("create_time");
  91. Integer update_time2 = (Integer) content.get("update_time");
  92. System.out.println("第二层解析");
  93. System.out.println("content:"+content.toJSONString());
  94. System.out.println("create_time:"+create_time);
  95. System.out.println("update_time2:"+update_time2);
  96. //3.第三层解析
  97. for(int j=0;j<news_item.size();j++){
  98. JSONObject map3 = news_item.getJSONObject(j);
  99. String title = (String) map3.get("title");
  100. String author = (String) map3.get("author");
  101. String content_source_url = (String) map3.get("content_source_url");
  102. Integer need_open_comment = (Integer) map3.get("need_open_comment");
  103. System.out.println("第三层解析");
  104. System.out.println("title:"+title);
  105. System.out.println("author:"+author);
  106. System.out.println("content_source_url:"+content_source_url);
  107. System.out.println("need_open_comment:"+need_open_comment);
  108. }
  109. }
  110. }
  111. }

结果:大致如图

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lpZ3VhbmdfODIw_size_16_color_FFFFFF_t_70 1

结论:主要使用两个对象的方法

  1. JSONObject map1 = JSON.parseObject(jsonStr); //map对象
  2. JSONArray item = map1.getJSONArray("item"); //数组对象

注意:map获值的类型:字符串或者基本数据类型,注意数据类型
watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lpZ3VhbmdfODIw_size_16_color_FFFFFF_t_70 2

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lpZ3VhbmdfODIw_size_16_color_FFFFFF_t_70 3

方法2:gons解析

  1. Gson g1=new Gson();
  2. Map m3 = g1.fromJson(jsonStr, HashMap.class);
  3. System.out.println(m3.toString());
  4. System.out.println(m3.get("item_count"));
  5. System.out.println(m3.get("item"));
  6. ArrayList l1=(ArrayList) m3.get("item");
  7. for(int i=0;i<l1.size();i++){
  8. Map m4 = (Map)l1.get(i);
  9. System.out.println("update_time:"+m4.get("update_time"));
  10. }

发表评论

表情:
评论列表 (有 0 条评论,381人围观)

还没有评论,来说两句吧...

相关阅读

    相关 复杂JSON字符串

    网上找了一些复杂JSON字符串解析的,这篇写的不错,简单简洁。 对于解析json字符串,确实有很大的帮助。 但是这是重在解析,其实还是要映射到实际的实体类中,这样才能保

    相关 JSON

        [ JSON 使用讲解 ][JSON _]这篇文章讲解了,JSON的介绍以及使用GSON解析。今天,我们就在Android项目中使用两种方式解析JSON数据。如果你对J