fastjson 判断是否包含_fastjson如何判断JSONObject和JSONArray

news/2024/7/5 21:15:16

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 classOrgJsonUtils {8

9 /**10 * 单层的orgJson判断是否是JSONObject还是JSONArray.11 */

12 public static voidsimpleJSONObjectOrgJson() {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 = newJSONTokener(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 voidsimpleJSONArrayOrgJson() {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 = newJSONTokener(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 voiddoubleOrgJson() {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 = newJSONObject(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:" +type99 + ", condition:" + condition + "}");100

101 //解析第二层,如果自己已经明确第二层是[]是JSONArray类型的,如下解析即可

102 JSONArray jsonArray2 = jsonObject.getJSONArray("data");103 //解析第二层,如果自己已经明确第二层是{}是JSONObject类型的,如下解析即可104 //JSONObject jsonObject2 = jsonObject.getJSONObject("data");

105

106

107 for (int i = 0; i < jsonArray2.length(); i++) {108 JSONObject jsonArrayObject = (JSONObject) jsonArray2.get(i);109 String DataSource = jsonArrayObject.getString("DataSource");110 String DataStamp = jsonArrayObject.getString("DataStamp");111 String OrgName = jsonArrayObject.getString("OrgName");112 String RegAddress = jsonArrayObject.getString("RegAddress");113 //...等等字段

114 System.out.println("{DataSource:" + DataSource + ", DataStamp:" + DataStamp + ", OrgName:" +OrgName115 + ", RegAddress:" + RegAddress + "}");116 }117 }118

119 public static voidmain(String[] args) {120 doubleOrgJson();121 //simpleJSONObjectOrgJson();122 //simpleJSONArrayOrgJson();

123 }124

125 }


http://www.niftyadmin.cn/n/3528642.html

相关文章

mysql优化小点

1。设置栏位名时&#xff0c;不要用关键字。如果用了关键字&#xff0c;尽量避免不必要的时候查询这个字段。sql解析会对该栏位名反复解析&#xff0c;导致查询奇慢转载于:https://www.cnblogs.com/xlban163/p/9968338.html

“2018中国企业互联网CEO峰会”听e签宝金宏洲讲新趋势

很多年前就有“移动互联网的流量红利已经消失”的说法&#xff0c;许多说法认为&#xff0c;一线城市的人都完成了从功能机到智能机的转化&#xff0c;互联网应用的使用习惯也逐步稳定&#xff0c;大量针对一线城市人群的新应用也难以获得用户流量。然后这时候大量三四五线城市…

C++多线程学习(十二、特殊的原子类型atomic_flag,自旋锁)

目录 atomic_flag 自旋锁 自旋锁与互斥锁的不同 1. 等待方式不同&#xff1a; 2. 资源消耗不同&#xff1a; 3. 适用场景不同&#xff1a; 简单案例 其他的原子类型是可以通过is_lock_free()来判定是否无锁 atomic_flag atomic_flag&#xff1a;是无锁的 atomic_flag的…

用法 stl_每日干货丨STL string容器用法

STL string容器用法概述string是C标准库的一个重要的部分&#xff0c;主要用于字符串处理。C的算法库对string类也有着很好的支持&#xff0c;并且string类还和c语言的字符串之间有着良好的接口。string概念string是STL的字符串类型&#xff0c;通常用来表示字符串。而在使用st…

的字符串连接_ABP vNext框架文档解读7-连接字符串

ABP框架的设计是模块化的, 微服务兼容 和 多租户. 同时设计了连接字符串管理来支持这些场景;允许为每个模块设置单独的连接字符串,这样每个模块都可以有自己的物理数据库. 甚至可以将模块配置为使用不同的DBMS.允许为每个租户设置单独的连接字符串使用单独的数据库(在SaaS应用程…

jchdl - RTL实例 - MOS6502 SoC

https://mp.weixin.qq.com/s/H2UBmZa9fpM6_FM2_MucTQ 实现一个SoC作为顶层模块&#xff0c;包含Cpu、Mem两个子模块&#xff0c;并驱动运行。参考链接https://github.com/wjcdx/jchdl/blob/master/src/org/jchdl/model/rtl/example/Mos6502/Soc.java1.创建Soc.java, 并生成构造…

供应商去市网维护银行账号信息_进销存软件有什么好处?对供应商管理的价值是什么?...

进销存软件有什么好处&#xff1f;对供应商管理的价值是什么对于贸易类企业来说&#xff0c;货品的采购&#xff0c;直接影响到企业后期产品的售价乃至企业利润&#xff0c;所以&#xff0c;采购很是重要&#xff0c;而对采购的优化当中&#xff0c;供应商的管理至关重要&#…

元素的属性

标签  元素  节点  对象所有标签的集合 元素属性 client系列 offset系列 scroll系列 案列&#xff1a; <div id"divs" class"aa bb cc"> </div> 脚本代码&#xff1a; var divdocument.getElementById…