fastjson如何判断JSONObject和JSONArray
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.47</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\":