1、fastjson如何判断JSONObject和JSONArray,百度一下,教程还真不少,但是是阿里的fastjson的我是没有找到合适的方法。这里用一个还算可以的方法,算是实现了这个效果。

网上贴代码,有时候不把引入的包贴上去,自己使用的话还真不知道是导入那个包咧。

maven依赖的如下所示:

  1. <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>fastjson</artifactId>
  5. <version>1.2.</version>
  6. </dependency>

实现代码如下所示:

例子主要实现的是解析第一层,然后解析第二层,解析第二层的时候判断是否是JSONArray还是JSONObject类型的。最后我是直接输出的,你肯定是将解析出的信息进行其他操作,这里不做叙述了。

  1. package com.fline.aic.utils;
  2.  
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5.  
  6. public class FastJosnUtils {
  7.  
  8. /**
  9. * FastJSON
  10. */
  11. public static void fastJson() {
  12. // 1、Json字符串,JSONObject类型的
  13. // String message = "{\r\n" + " \"catalogId\": \"IJ1009\",\r\n"
  14. // + " \"tableName\": \"core_data.uc_gov_org\",\r\n" + " \"type\": \"POST\",\r\n"
  15. // + " \"condition\": \"null\",\r\n" + " \"data\": {\r\n" + " \"DataSource\": \"'P33'\",\r\n"
  16. // + " \"DataStamp\": \"'2018-08-25'\",\r\n" + " \"GovScCode\": \"'aaa'\",\r\n"
  17. // + " \"OperEffDate\": \"'2018-05-02'\",\r\n" + " \"OrgCode\": \"'ww'\",\r\n"
  18. // + " \"OrgDesc\": \"'ss'\",\r\n" + " \"OrgId\": \"1\",\r\n"
  19. // + " \"OrgName\": \"'11111'\",\r\n" + " \"OrgSeq\": \"11\",\r\n"
  20. // + " \"OrgShortName\": \"'ss'\",\r\n" + " \"OrgStatus\": \"'ss'\",\r\n"
  21. // + " \"OrgType\": \"'ss'\",\r\n" + " \"ParentOrgId\": \"0\",\r\n"
  22. // + " \"RegAddress\": \"'ww'\",\r\n" + " \"RegDate\": \"\",\r\n" + " \"RegionId\": \"1\"\r\n"
  23. // + " }\r\n" + "}";
  24.  
  25. // 1、Json字符串,JSONArray类型的
  26. String message = "{\r\n" + " \"catalogId\": \"IJ1009\",\r\n"
  27. + " \"tableName\": \"core_data.uc_gov_org\",\r\n" + " \"type\": \"POST\",\r\n"
  28. + " \"condition\": \"null\",\r\n" + " \"data\": [{\r\n" + " \"DataSource\": \"'P33'\",\r\n"
  29. + " \"DataStamp\": \"'2018-08-25'\",\r\n" + " \"GovScCode\": \"'aaa'\",\r\n"
  30. + " \"OperEffDate\": \"'2018-05-02'\",\r\n" + " \"OrgCode\": \"'ww'\",\r\n"
  31. + " \"OrgDesc\": \"'ss'\",\r\n" + " \"OrgId\": \"1\",\r\n"
  32. + " \"OrgName\": \"'11111'\",\r\n" + " \"OrgSeq\": \"11\",\r\n"
  33. + " \"OrgShortName\": \"'ss'\",\r\n" + " \"OrgStatus\": \"'ss'\",\r\n"
  34. + " \"OrgType\": \"'ss'\",\r\n" + " \"ParentOrgId\": \"0\",\r\n"
  35. + " \"RegAddress\": \"'ww'\",\r\n" + " \"RegDate\": \"\",\r\n" + " \"RegionId\": \"1\"\r\n"
  36. + " },{\r\n" +
  37. " \"DataSource\": \"'P33'\",\r\n" +
  38. " \"DataStamp\": \"'2018-08-25'\",\r\n" +
  39. " \"GovScCode\": \"'aaa'\",\r\n" +
  40. " \"OperEffDate\": \"'2018-05-02'\",\r\n" +
  41. " \"OrgCode\": \"'ww'\",\r\n" +
  42. " \"OrgDesc\": \"'ss'\",\r\n" +
  43. " \"OrgId\": \"1\",\r\n" +
  44. " \"OrgName\": \"'11111'\",\r\n" +
  45. " \"OrgSeq\": \"11\",\r\n" +
  46. " \"OrgShortName\": \"'ss'\",\r\n" +
  47. " \"OrgStatus\": \"'ss'\",\r\n" +
  48. " \"OrgType\": \"'ss'\",\r\n" +
  49. " \"ParentOrgId\": \"0\",\r\n" +
  50. " \"RegAddress\": \"'ww'\",\r\n" +
  51. " \"RegDate\": \"\",\r\n" +
  52. " \"RegionId\": \"1\"\r\n" +
  53. " }]\r\n" + "}";
  54. // 解析第一层{},由于多种json写到了一起,所以直接引用到了包,自行开发引用自己的Json包就行。
  55. JSONObject jsonObject = JSONObject.parseObject(message);
  56. String catalogId = jsonObject.getString("catalogId");
  57. String schemaTableName = jsonObject.getString("tableName");
  58. String type = jsonObject.getString("type");
  59. String condition = jsonObject.getString("condition");
  60. System.out.println("{catalogId :" + catalogId + ", schemaTableName: " + schemaTableName + ", type: " + type
  61. + ", condition:" + condition + "}");
  62.  
  63. // 解析第二层,如果根据需求可以判断是否是JSONObject还是JSONArray
  64. JSONArray jsonArray = new JSONArray();
  65. JSONObject jsonData = new JSONObject();
  66. String data = jsonObject.getString("data");
  67. //百度了很对,也没有找到合适的,我是这样判断的是否是JSONArray还是JSONObject
  68. if (data.contains("[") && data.contains("]")) {
  69. jsonArray = JSONArray.parseArray(data);
  70. System.out.println("jsonArray: " + jsonArray);
  71. //然后可以解析第二层
  72. for(int i=;i< jsonArray.size();i++) {
  73. JSONObject jsonArrayObject = (JSONObject) jsonArray.get(i);
  74. String DataSource = jsonArrayObject.getString("DataSource");
  75. String DataStamp = jsonArrayObject.getString("DataStamp");
  76. String OrgName = jsonArrayObject.getString("OrgName");
  77. String RegAddress = jsonArrayObject.getString("RegAddress");
  78. //...等等字段
  79. System.out.println("{DataSource: "+DataSource +", DataStamp: " + DataStamp + ", OrgName: " +OrgName +", RegAddress: " + RegAddress);
  80. }
  81.  
  82. } else {
  83. jsonData = JSONObject.parseObject(data);
  84. System.out.println("jsonData: " + jsonData);
  85. //然后可以解析第二层
  86. String DataSource = jsonData.getString("DataSource");
  87. String DataStamp = jsonData.getString("DataStamp");
  88. String OrgName = jsonData.getString("OrgName");
  89. String RegAddress = jsonData.getString("RegAddress");
  90. //...等等字段
  91. System.out.println("{DataSource: "+DataSource +", DataStamp: " + DataStamp + ", OrgName: " +OrgName +", RegAddress: " + RegAddress);
  92. }
  93. }
  94.  
  95. public static void main(String[] args) {
  96. fastJson();
  97.  
  98. }
  99. }

2、JSON官方的判断json字符串是JSONArray还是JSONObject类型的。是这个样子搞得,百度一下,教程还是很多的。这里也简单贴一下。

  1. <!-- https://mvnrepository.com/artifact/org.json/json -->
  2. <dependency>
  3. <groupId>org.json</groupId>
  4. <artifactId>json</artifactId>
  5. <version></version>
  6. </dependency>

案例代码如下所示:

  1. package com.fline.aic.utils;
  2.  
  3. import org.json.JSONArray;
  4. import org.json.JSONObject;
  5. import org.json.JSONTokener;
  6.  
  7. public class OrgJsonUtils {
  8.  
  9. /**
  10. * 单层的orgJson判断是否是JSONObject还是JSONArray.
  11. */
  12. public static void simpleJSONObjectOrgJson() {
  13. String message = "[{\r\n" + " \"DataSource\": \"'P33'\",\r\n"
  14. + " \"DataStamp\": \"'2018-08-25'\",\r\n" + " \"GovScCode\": \"'aaa'\",\r\n"
  15. + " \"OperEffDate\": \"'2018-05-02'\",\r\n" + " \"OrgCode\": \"'ww'\",\r\n"
  16. + " \"OrgDesc\": \"'ss'\",\r\n" + " \"OrgId\": \"1\",\r\n"
  17. + " \"OrgName\": \"'11111'\",\r\n" + " \"OrgSeq\": \"11\",\r\n"
  18. + " \"OrgShortName\": \"'ss'\",\r\n" + " \"OrgStatus\": \"'ss'\",\r\n"
  19. + " \"OrgType\": \"'ss'\",\r\n" + " \"ParentOrgId\": \"0\",\r\n"
  20. + " \"RegAddress\": \"'ww'\",\r\n" + " \"RegDate\": \"\",\r\n" + " \"RegionId\": \"1\"\r\n"
  21. + " }]";
  22. Object json = new JSONTokener(message).nextValue();
  23. if (json instanceof JSONObject) {
  24. JSONObject jsonObject = (JSONObject) json;
  25. System.out.println(jsonObject);
  26. //自行解析即可
  27. } else if (json instanceof JSONArray) {
  28. JSONArray jsonArray = (JSONArray) json;
  29. System.out.println(jsonArray);
  30. //自行解析即可
  31. }
  32. }
  33.  
  34. /**
  35. * 单层的orgJson判断是否是JSONObject还是JSONArray.
  36. */
  37. public static void simpleJSONArrayOrgJson() {
  38. String message = "{\r\n" + " \"DataSource\": \"'P33'\",\r\n"
  39. + " \"DataStamp\": \"'2018-08-25'\",\r\n" + " \"GovScCode\": \"'aaa'\",\r\n"
  40. + " \"OperEffDate\": \"'2018-05-02'\",\r\n" + " \"OrgCode\": \"'ww'\",\r\n"
  41. + " \"OrgDesc\": \"'ss'\",\r\n" + " \"OrgId\": \"1\",\r\n"
  42. + " \"OrgName\": \"'11111'\",\r\n" + " \"OrgSeq\": \"11\",\r\n"
  43. + " \"OrgShortName\": \"'ss'\",\r\n" + " \"OrgStatus\": \"'ss'\",\r\n"
  44. + " \"OrgType\": \"'ss'\",\r\n" + " \"ParentOrgId\": \"0\",\r\n"
  45. + " \"RegAddress\": \"'ww'\",\r\n" + " \"RegDate\": \"\",\r\n" + " \"RegionId\": \"1\"\r\n"
  46. + " }";
  47. Object json = new JSONTokener(message).nextValue();
  48. if (json instanceof JSONObject) {
  49. JSONObject jsonObject = (JSONObject) json;
  50. System.out.println(jsonObject);
  51. //自行解析即可
  52. } else if (json instanceof JSONArray) {
  53. JSONArray jsonArray = (JSONArray) json;
  54. System.out.println(jsonArray);
  55. //自行解析即可
  56. }
  57. }
  58.  
  59. /**
  60. * JSON官方
  61. */
  62. public static void doubleOrgJson() {
  63. // Json字符串
  64. /*
  65. * String message = "{\r\n" + " \"catalogId\": \"IJ1009\",\r\n" +
  66. * " \"tableName\": \"core_data.uc_gov_org\",\r\n" +
  67. * " \"type\": \"POST\",\r\n" + " \"condition\": \"null\",\r\n" +
  68. * " \"data\": {\r\n" + " \"DataSource\": \"'P33'\",\r\n" +
  69. * " \"DataStamp\": \"'2018-08-25'\",\r\n" +
  70. * " \"GovScCode\": \"'aaa'\",\r\n" +
  71. * " \"OperEffDate\": \"'2018-05-02'\",\r\n" +
  72. * " \"OrgCode\": \"'ww'\",\r\n" + " \"OrgDesc\": \"'ss'\",\r\n" +
  73. * " \"OrgId\": \"1\",\r\n" + " \"OrgName\": \"'11111'\",\r\n" +
  74. * " \"OrgSeq\": \"11\",\r\n" + " \"OrgShortName\": \"'ss'\",\r\n"
  75. * + " \"OrgStatus\": \"'ss'\",\r\n" + " \"OrgType\": \"'ss'\",\r\n"
  76. * + " \"ParentOrgId\": \"0\",\r\n" +
  77. * " \"RegAddress\": \"'ww'\",\r\n" + " \"RegDate\": \"\",\r\n" +
  78. * " \"RegionId\": \"1\"\r\n" + " }\r\n" + "}";
  79. */
  80.  
  81. String message = "{\r\n" + " \"catalogId\": \"IJ1009\",\r\n"
  82. + " \"tableName\": \"core_data.uc_gov_org\",\r\n" + " \"type\": \"POST\",\r\n"
  83. + " \"condition\": \"null\",\r\n" + " \"data\": [{\r\n" + " \"DataSource\": \"'P33'\",\r\n"
  84. + " \"DataStamp\": \"'2018-08-25'\",\r\n" + " \"GovScCode\": \"'aaa'\",\r\n"
  85. + " \"OperEffDate\": \"'2018-05-02'\",\r\n" + " \"OrgCode\": \"'ww'\",\r\n"
  86. + " \"OrgDesc\": \"'ss'\",\r\n" + " \"OrgId\": \"1\",\r\n"
  87. + " \"OrgName\": \"'11111'\",\r\n" + " \"OrgSeq\": \"11\",\r\n"
  88. + " \"OrgShortName\": \"'ss'\",\r\n" + " \"OrgStatus\": \"'ss'\",\r\n"
  89. + " \"OrgType\": \"'ss'\",\r\n" + " \"ParentOrgId\": \"0\",\r\n"
  90. + " \"RegAddress\": \"'ww'\",\r\n" + " \"RegDate\": \"\",\r\n" + " \"RegionId\": \"1\"\r\n"
  91. + " }]\r\n" + "}";
  92. // 解析第一层{}
  93. JSONObject jsonObject = new JSONObject(message);
  94. String catalogId = jsonObject.getString("catalogId");
  95. String schemaTableName = jsonObject.getString("tableName");
  96. String type = jsonObject.getString("type");
  97. String condition = jsonObject.getString("condition");
  98. System.out.println("{catalogId :" + catalogId + ", schemaTableName: " + schemaTableName + ", type: " + type
  99. + ", condition:" + condition + "}");
  100.  
  101. // 解析第二层,如果自己已经明确第二层是[]是JSONArray类型的,如下解析即可
  102. JSONArray jsonArray2 = jsonObject.getJSONArray("data");
  103. // 解析第二层,如果自己已经明确第二层是{}是JSONObject类型的,如下解析即可
  104. // JSONObject jsonObject2 = jsonObject.getJSONObject("data");
  105.  
  106. for (int i = ; i < jsonArray2.length(); i++) {
  107. JSONObject jsonArrayObject = (JSONObject) jsonArray2.get(i);
  108. String DataSource = jsonArrayObject.getString("DataSource");
  109. String DataStamp = jsonArrayObject.getString("DataStamp");
  110. String OrgName = jsonArrayObject.getString("OrgName");
  111. String RegAddress = jsonArrayObject.getString("RegAddress");
  112. // ...等等字段
  113. System.out.println("{DataSource: " + DataSource + ", DataStamp: " + DataStamp + ", OrgName: " + OrgName
  114. + ", RegAddress: " + RegAddress + "}");
  115. }
  116. }
  117.  
  118. public static void main(String[] args) {
  119. doubleOrgJson();
  120. // simpleJSONObjectOrgJson();
  121. // simpleJSONArrayOrgJson();
  122. }
  123.  
  124. }

GSON和jackson,我还未接触过,这里就不叙述了,百度也有很多教程。

待续.....

fastjson如何判断JSONObject和JSONArray的更多相关文章

  1. java判断jsonObject和jsonArray是否为空

    resJsonObj = {"res":"0","msg":"","data":{"Nam ...

  2. Fastjson, Gson, org.json.JSON三者对于JSONObject及JSONArray的判断

    1.Fastjson 我们通常在已知格式的情况下直接使用JSONObject,JSONArray,但是如果遇到需要判断格式呢? try{ Object object = JSON.parse(a); ...

  3. fastjson之JSONObject、JSONArray

    JSONObject,JSONArray是JSON的两个子类. 首先我们来看JSONObject源码: 会发现JSONObject是继承Map<String, Object>,并且都是使用 ...

  4. Java学习-030-JSON 之四 -- 判断 JSONObject 是否包含键值对

    前文对获取 JSON 数据封装方法,使之可通过类似于 cssSelector 的方法获取 JSON 数据,使获取数据变得简单.敬请参阅:模仿 cssSelector 封装读取 JSON 数据方法. 在 ...

  5. Gson基本操作,JsonObject,JsonArray,String,JavaBean,List互转

    (转自)https://www.cnblogs.com/robbinluobo/p/7217387.html String.JsonObject.JavaBean 互相转换 User user = n ...

  6. JSONObject 和 JSONArray 的区别和用法

    JSONObject 和 JSONArray 的数据表现形式不同: JSONObject的数据是用 {  } 来表示的,例如: { "id" : "1", &q ...

  7. 011-JSON、JSONObject、JSONArray使用、JSON数组形式字符串转换为List<Map<String,String>>的8种方法

    一.JSON数据格式 1.1.常用JSON数据格式 1.对象方式:JSONObject的数据是用 { } 来表示的, 例如: { "id" : "123", & ...

  8. JsonObject、JsonArray操作json的个人总结

    介绍 JsonObject.JsonArray之前,先介绍下JsonConfig JsonConfig: setClassMap(Map classMap)设置json属性类型,上json里的其中值为 ...

  9. json学习系列(6)JSONObject和JSONArray是JDK的集合部分延伸

    我一直觉得JSONObject和JSONArray是JDK集合部分的延伸,它们与JDK的List和Map一脉相承.通过研究JSONObject和JSONArray的结构,我们顺便也复习一下JDK的内容 ...

随机推荐

  1. k64 datasheet学习笔记45---10/100-Mbps Ethernet MAC(ENET)之概述

    1.前言 k64 ENET CORE 实现了10M/100Mbps的Ethernet MAC,与IEEE802.3-2002标准兼容. MAC层与全双工/半双工的10M/100Mbps以太网兼容: M ...

  2. hibernate框架学习之数据查询(QBC)helloworld

    package cn.itcast.h3.query.hql; import java.util.List; import org.hibernate.Criteria; import org.hib ...

  3. ThreadLocal与Synchronized区别

    ThreadLocal和Synchonized都用于解决多线程并发访问他们两者的区别:synchronized是利用锁的机制,使变量或代码块在某一时该只能被一个线程访问,而ThreadLocal为每一 ...

  4. Chrome表单文本框自动填充黄色背景色样式

    chrome表单自动填充后,input文本框的背景会变成偏黄色的,这是由于chrome会默认给自动填充的input表单加上input:-webkit-autofill私有属性,然后对其赋予以下样式: ...

  5. MySQL--(了解)可能会用到的内置函数

    mysql内置函数列表可以从mysql官方文档查询,这里仅分类简单介绍一些可能会用到的函数.1 数学函数abs(x)pi()mod(x,y)sqrt(x)ceil(x)或者ceiling(x)rand ...

  6. webservice:com.sun.xml.internal.ws.server.ServerRtException: [failed to localize]

    发布webservice发生了错误,一直没有能够解决,错误如下: Exception in thread "main" com.sun.xml.internal.ws.server ...

  7. Netflix正式开源其API网关Zuul 2

    5 月 21 日,Netflix 在其官方博客上宣布正式开源微服务网关组件 Zuul 2.Netflix 公司是微服务界的楷模,他们有大规模生产级微服务的成功应用案例,也开源了相当多的微服务组件(详见 ...

  8. VUE 密码验证与提示

    1. 概述 1.1 说明 vue项目中,为了较为明了的让用户看到所输入的密码信息的长度与复杂度是否满足要求,开发一个组件来满足此需求(当密码输入时进行密码验证操作,当密码的长度在8到24位之间,密码中 ...

  9. Affiliate实战记录之一:CPI、CPA、CPM...名词解释

    1.CPM (Cost Per Mille,或者Cost Per Thousand;Cost Per Impressions) 每千人成本,按展示次数收费 网上广告收费最科学的办法是按照有多少人看到你 ...

  10. PID控制器开发笔记之十:步进式PID控制器的实现

    对于一般的PID控制系统来说,当设定值发生较大的突变时,很容易产生超调而使系统不稳定.为了解决这种阶跃变化造成的不利影响,人们发明了步进式PID控制算法. 1.步进式PID的基本思想 所谓步进式PID ...