fastjson如何判断JSONObject和JSONArray
1、fastjson如何判断JSONObject和JSONArray,百度一下,教程还真不少,但是是阿里的fastjson的我是没有找到合适的方法。这里用一个还算可以的方法,算是实现了这个效果。
网上贴代码,有时候不把引入的包贴上去,自己使用的话还真不知道是导入那个包咧。
maven依赖的如下所示:
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.</version>
</dependency>
实现代码如下所示:
例子主要实现的是解析第一层,然后解析第二层,解析第二层的时候判断是否是JSONArray还是JSONObject类型的。最后我是直接输出的,你肯定是将解析出的信息进行其他操作,这里不做叙述了。
package com.fline.aic.utils; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; public class FastJosnUtils { /**
* FastJSON
*/
public static void fastJson() {
// 1、Json字符串,JSONObject类型的
// String message = "{\r\n" + " \"catalogId\": \"IJ1009\",\r\n"
// + " \"tableName\": \"core_data.uc_gov_org\",\r\n" + " \"type\": \"POST\",\r\n"
// + " \"condition\": \"null\",\r\n" + " \"data\": {\r\n" + " \"DataSource\": \"'P33'\",\r\n"
// + " \"DataStamp\": \"'2018-08-25'\",\r\n" + " \"GovScCode\": \"'aaa'\",\r\n"
// + " \"OperEffDate\": \"'2018-05-02'\",\r\n" + " \"OrgCode\": \"'ww'\",\r\n"
// + " \"OrgDesc\": \"'ss'\",\r\n" + " \"OrgId\": \"1\",\r\n"
// + " \"OrgName\": \"'11111'\",\r\n" + " \"OrgSeq\": \"11\",\r\n"
// + " \"OrgShortName\": \"'ss'\",\r\n" + " \"OrgStatus\": \"'ss'\",\r\n"
// + " \"OrgType\": \"'ss'\",\r\n" + " \"ParentOrgId\": \"0\",\r\n"
// + " \"RegAddress\": \"'ww'\",\r\n" + " \"RegDate\": \"\",\r\n" + " \"RegionId\": \"1\"\r\n"
// + " }\r\n" + "}"; // 1、Json字符串,JSONArray类型的
String message = "{\r\n" + " \"catalogId\": \"IJ1009\",\r\n"
+ " \"tableName\": \"core_data.uc_gov_org\",\r\n" + " \"type\": \"POST\",\r\n"
+ " \"condition\": \"null\",\r\n" + " \"data\": [{\r\n" + " \"DataSource\": \"'P33'\",\r\n"
+ " \"DataStamp\": \"'2018-08-25'\",\r\n" + " \"GovScCode\": \"'aaa'\",\r\n"
+ " \"OperEffDate\": \"'2018-05-02'\",\r\n" + " \"OrgCode\": \"'ww'\",\r\n"
+ " \"OrgDesc\": \"'ss'\",\r\n" + " \"OrgId\": \"1\",\r\n"
+ " \"OrgName\": \"'11111'\",\r\n" + " \"OrgSeq\": \"11\",\r\n"
+ " \"OrgShortName\": \"'ss'\",\r\n" + " \"OrgStatus\": \"'ss'\",\r\n"
+ " \"OrgType\": \"'ss'\",\r\n" + " \"ParentOrgId\": \"0\",\r\n"
+ " \"RegAddress\": \"'ww'\",\r\n" + " \"RegDate\": \"\",\r\n" + " \"RegionId\": \"1\"\r\n"
+ " },{\r\n" +
" \"DataSource\": \"'P33'\",\r\n" +
" \"DataStamp\": \"'2018-08-25'\",\r\n" +
" \"GovScCode\": \"'aaa'\",\r\n" +
" \"OperEffDate\": \"'2018-05-02'\",\r\n" +
" \"OrgCode\": \"'ww'\",\r\n" +
" \"OrgDesc\": \"'ss'\",\r\n" +
" \"OrgId\": \"1\",\r\n" +
" \"OrgName\": \"'11111'\",\r\n" +
" \"OrgSeq\": \"11\",\r\n" +
" \"OrgShortName\": \"'ss'\",\r\n" +
" \"OrgStatus\": \"'ss'\",\r\n" +
" \"OrgType\": \"'ss'\",\r\n" +
" \"ParentOrgId\": \"0\",\r\n" +
" \"RegAddress\": \"'ww'\",\r\n" +
" \"RegDate\": \"\",\r\n" +
" \"RegionId\": \"1\"\r\n" +
" }]\r\n" + "}";
// 解析第一层{},由于多种json写到了一起,所以直接引用到了包,自行开发引用自己的Json包就行。
JSONObject jsonObject = JSONObject.parseObject(message);
String catalogId = jsonObject.getString("catalogId");
String schemaTableName = jsonObject.getString("tableName");
String type = jsonObject.getString("type");
String condition = jsonObject.getString("condition");
System.out.println("{catalogId :" + catalogId + ", schemaTableName: " + schemaTableName + ", type: " + type
+ ", condition:" + condition + "}"); // 解析第二层,如果根据需求可以判断是否是JSONObject还是JSONArray
JSONArray jsonArray = new JSONArray();
JSONObject jsonData = new JSONObject();
String data = jsonObject.getString("data");
//百度了很对,也没有找到合适的,我是这样判断的是否是JSONArray还是JSONObject
if (data.contains("[") && data.contains("]")) {
jsonArray = JSONArray.parseArray(data);
System.out.println("jsonArray: " + jsonArray);
//然后可以解析第二层
for(int i=;i< jsonArray.size();i++) {
JSONObject jsonArrayObject = (JSONObject) jsonArray.get(i);
String DataSource = jsonArrayObject.getString("DataSource");
String DataStamp = jsonArrayObject.getString("DataStamp");
String OrgName = jsonArrayObject.getString("OrgName");
String RegAddress = jsonArrayObject.getString("RegAddress");
//...等等字段
System.out.println("{DataSource: "+DataSource +", DataStamp: " + DataStamp + ", OrgName: " +OrgName +", RegAddress: " + RegAddress);
} } else {
jsonData = JSONObject.parseObject(data);
System.out.println("jsonData: " + jsonData);
//然后可以解析第二层
String DataSource = jsonData.getString("DataSource");
String DataStamp = jsonData.getString("DataStamp");
String OrgName = jsonData.getString("OrgName");
String RegAddress = jsonData.getString("RegAddress");
//...等等字段
System.out.println("{DataSource: "+DataSource +", DataStamp: " + DataStamp + ", OrgName: " +OrgName +", RegAddress: " + RegAddress);
}
} public static void main(String[] args) {
fastJson(); }
}
2、JSON官方的判断json字符串是JSONArray还是JSONObject类型的。是这个样子搞得,百度一下,教程还是很多的。这里也简单贴一下。
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version></version>
</dependency>
案例代码如下所示:
package com.fline.aic.utils; import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener; public class OrgJsonUtils { /**
* 单层的orgJson判断是否是JSONObject还是JSONArray.
*/
public static void simpleJSONObjectOrgJson() {
String message = "[{\r\n" + " \"DataSource\": \"'P33'\",\r\n"
+ " \"DataStamp\": \"'2018-08-25'\",\r\n" + " \"GovScCode\": \"'aaa'\",\r\n"
+ " \"OperEffDate\": \"'2018-05-02'\",\r\n" + " \"OrgCode\": \"'ww'\",\r\n"
+ " \"OrgDesc\": \"'ss'\",\r\n" + " \"OrgId\": \"1\",\r\n"
+ " \"OrgName\": \"'11111'\",\r\n" + " \"OrgSeq\": \"11\",\r\n"
+ " \"OrgShortName\": \"'ss'\",\r\n" + " \"OrgStatus\": \"'ss'\",\r\n"
+ " \"OrgType\": \"'ss'\",\r\n" + " \"ParentOrgId\": \"0\",\r\n"
+ " \"RegAddress\": \"'ww'\",\r\n" + " \"RegDate\": \"\",\r\n" + " \"RegionId\": \"1\"\r\n"
+ " }]";
Object json = new JSONTokener(message).nextValue();
if (json instanceof JSONObject) {
JSONObject jsonObject = (JSONObject) json;
System.out.println(jsonObject);
//自行解析即可
} else if (json instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) json;
System.out.println(jsonArray);
//自行解析即可
}
} /**
* 单层的orgJson判断是否是JSONObject还是JSONArray.
*/
public static void simpleJSONArrayOrgJson() {
String message = "{\r\n" + " \"DataSource\": \"'P33'\",\r\n"
+ " \"DataStamp\": \"'2018-08-25'\",\r\n" + " \"GovScCode\": \"'aaa'\",\r\n"
+ " \"OperEffDate\": \"'2018-05-02'\",\r\n" + " \"OrgCode\": \"'ww'\",\r\n"
+ " \"OrgDesc\": \"'ss'\",\r\n" + " \"OrgId\": \"1\",\r\n"
+ " \"OrgName\": \"'11111'\",\r\n" + " \"OrgSeq\": \"11\",\r\n"
+ " \"OrgShortName\": \"'ss'\",\r\n" + " \"OrgStatus\": \"'ss'\",\r\n"
+ " \"OrgType\": \"'ss'\",\r\n" + " \"ParentOrgId\": \"0\",\r\n"
+ " \"RegAddress\": \"'ww'\",\r\n" + " \"RegDate\": \"\",\r\n" + " \"RegionId\": \"1\"\r\n"
+ " }";
Object json = new JSONTokener(message).nextValue();
if (json instanceof JSONObject) {
JSONObject jsonObject = (JSONObject) json;
System.out.println(jsonObject);
//自行解析即可
} else if (json instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) json;
System.out.println(jsonArray);
//自行解析即可
}
} /**
* JSON官方
*/
public static void doubleOrgJson() {
// Json字符串
/*
* String message = "{\r\n" + " \"catalogId\": \"IJ1009\",\r\n" +
* " \"tableName\": \"core_data.uc_gov_org\",\r\n" +
* " \"type\": \"POST\",\r\n" + " \"condition\": \"null\",\r\n" +
* " \"data\": {\r\n" + " \"DataSource\": \"'P33'\",\r\n" +
* " \"DataStamp\": \"'2018-08-25'\",\r\n" +
* " \"GovScCode\": \"'aaa'\",\r\n" +
* " \"OperEffDate\": \"'2018-05-02'\",\r\n" +
* " \"OrgCode\": \"'ww'\",\r\n" + " \"OrgDesc\": \"'ss'\",\r\n" +
* " \"OrgId\": \"1\",\r\n" + " \"OrgName\": \"'11111'\",\r\n" +
* " \"OrgSeq\": \"11\",\r\n" + " \"OrgShortName\": \"'ss'\",\r\n"
* + " \"OrgStatus\": \"'ss'\",\r\n" + " \"OrgType\": \"'ss'\",\r\n"
* + " \"ParentOrgId\": \"0\",\r\n" +
* " \"RegAddress\": \"'ww'\",\r\n" + " \"RegDate\": \"\",\r\n" +
* " \"RegionId\": \"1\"\r\n" + " }\r\n" + "}";
*/ String message = "{\r\n" + " \"catalogId\": \"IJ1009\",\r\n"
+ " \"tableName\": \"core_data.uc_gov_org\",\r\n" + " \"type\": \"POST\",\r\n"
+ " \"condition\": \"null\",\r\n" + " \"data\": [{\r\n" + " \"DataSource\": \"'P33'\",\r\n"
+ " \"DataStamp\": \"'2018-08-25'\",\r\n" + " \"GovScCode\": \"'aaa'\",\r\n"
+ " \"OperEffDate\": \"'2018-05-02'\",\r\n" + " \"OrgCode\": \"'ww'\",\r\n"
+ " \"OrgDesc\": \"'ss'\",\r\n" + " \"OrgId\": \"1\",\r\n"
+ " \"OrgName\": \"'11111'\",\r\n" + " \"OrgSeq\": \"11\",\r\n"
+ " \"OrgShortName\": \"'ss'\",\r\n" + " \"OrgStatus\": \"'ss'\",\r\n"
+ " \"OrgType\": \"'ss'\",\r\n" + " \"ParentOrgId\": \"0\",\r\n"
+ " \"RegAddress\": \"'ww'\",\r\n" + " \"RegDate\": \"\",\r\n" + " \"RegionId\": \"1\"\r\n"
+ " }]\r\n" + "}";
// 解析第一层{}
JSONObject jsonObject = new JSONObject(message);
String catalogId = jsonObject.getString("catalogId");
String schemaTableName = jsonObject.getString("tableName");
String type = jsonObject.getString("type");
String condition = jsonObject.getString("condition");
System.out.println("{catalogId :" + catalogId + ", schemaTableName: " + schemaTableName + ", type: " + type
+ ", condition:" + condition + "}"); // 解析第二层,如果自己已经明确第二层是[]是JSONArray类型的,如下解析即可
JSONArray jsonArray2 = jsonObject.getJSONArray("data");
// 解析第二层,如果自己已经明确第二层是{}是JSONObject类型的,如下解析即可
// JSONObject jsonObject2 = jsonObject.getJSONObject("data"); for (int i = ; i < jsonArray2.length(); i++) {
JSONObject jsonArrayObject = (JSONObject) jsonArray2.get(i);
String DataSource = jsonArrayObject.getString("DataSource");
String DataStamp = jsonArrayObject.getString("DataStamp");
String OrgName = jsonArrayObject.getString("OrgName");
String RegAddress = jsonArrayObject.getString("RegAddress");
// ...等等字段
System.out.println("{DataSource: " + DataSource + ", DataStamp: " + DataStamp + ", OrgName: " + OrgName
+ ", RegAddress: " + RegAddress + "}");
}
} public static void main(String[] args) {
doubleOrgJson();
// simpleJSONObjectOrgJson();
// simpleJSONArrayOrgJson();
} }
GSON和jackson,我还未接触过,这里就不叙述了,百度也有很多教程。
待续.....
fastjson如何判断JSONObject和JSONArray的更多相关文章
- java判断jsonObject和jsonArray是否为空
resJsonObj = {"res":"0","msg":"","data":{"Nam ...
- Fastjson, Gson, org.json.JSON三者对于JSONObject及JSONArray的判断
1.Fastjson 我们通常在已知格式的情况下直接使用JSONObject,JSONArray,但是如果遇到需要判断格式呢? try{ Object object = JSON.parse(a); ...
- fastjson之JSONObject、JSONArray
JSONObject,JSONArray是JSON的两个子类. 首先我们来看JSONObject源码: 会发现JSONObject是继承Map<String, Object>,并且都是使用 ...
- Java学习-030-JSON 之四 -- 判断 JSONObject 是否包含键值对
前文对获取 JSON 数据封装方法,使之可通过类似于 cssSelector 的方法获取 JSON 数据,使获取数据变得简单.敬请参阅:模仿 cssSelector 封装读取 JSON 数据方法. 在 ...
- Gson基本操作,JsonObject,JsonArray,String,JavaBean,List互转
(转自)https://www.cnblogs.com/robbinluobo/p/7217387.html String.JsonObject.JavaBean 互相转换 User user = n ...
- JSONObject 和 JSONArray 的区别和用法
JSONObject 和 JSONArray 的数据表现形式不同: JSONObject的数据是用 { } 来表示的,例如: { "id" : "1", &q ...
- 011-JSON、JSONObject、JSONArray使用、JSON数组形式字符串转换为List<Map<String,String>>的8种方法
一.JSON数据格式 1.1.常用JSON数据格式 1.对象方式:JSONObject的数据是用 { } 来表示的, 例如: { "id" : "123", & ...
- JsonObject、JsonArray操作json的个人总结
介绍 JsonObject.JsonArray之前,先介绍下JsonConfig JsonConfig: setClassMap(Map classMap)设置json属性类型,上json里的其中值为 ...
- json学习系列(6)JSONObject和JSONArray是JDK的集合部分延伸
我一直觉得JSONObject和JSONArray是JDK集合部分的延伸,它们与JDK的List和Map一脉相承.通过研究JSONObject和JSONArray的结构,我们顺便也复习一下JDK的内容 ...
随机推荐
- Fusebox 类似WEBPACK 的工具,React Studio
Fusebox 类似WEBPACK 的工具, http://fuse-box.org/ React Studio: https://hackernoon.com/@reactstudio
- 拿什么守护你的Node.JS进程: Node出错崩溃了怎么办?
被吐嘈的NodeJS的异常处理 许多人都有这样一种映像,NodeJS比较快: 但是因为其是单线程,所以它不稳定,有点不安全,不适合处理复杂业务: 它比较适合对并发要求比较高,而且简单的业务场景. 在E ...
- Nginx和apache服务器中php运行方式
PHP5的CGI方式的一大优势是内置了FastCGI的支持,只需指明绑定的地址和端口参数便可以以FastCGI的方式运行,如下: php-cgi -b 127.0.0.1:9000 配置Nginx的P ...
- Server-side activities have been updated. You need to restart SharePoint Designer to use the updated version of activities
一,环境: SharePoint Online Win10 + SharePoint Designer 2013 错误描述: 使用SPD3013打开SPO站点,然后编辑已有工作流(2013),报错: ...
- 第八次作业(课堂实战)- 项目UML设计(团队)
1. 团队信息 队名:小白吃队 成员: 后敬甲 031602409 卢泽明 031602328 蔡文斌 031602301 葛亮 031602617 刘浩 031602423 黄泽 031602317 ...
- Kendo ui 入门知识点
1. Kendo的继承 varPerson= kendo.Class.extend({...}); var person = new person(); var Parent = kendo.Clas ...
- 1-HTML Attributes
下表列举了常用的Html属性 Attribute Description alt Specifies an alternative text for an image, when the image ...
- Webapi 跨域 解决解决错误No 'Access-Control-Allow-Origin' header is present on the requested resource 问题
首先是web端(http://localhost:53784) 请求 api(http://localhost:81/api/)时出现错误信息: 查看控制台会发现错误:XMLHttpRequest c ...
- 后台调用前台js方法
后台调用前台jsClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<s ...
- Oracle的AES加密与解密用法
Oracle的AES加密与解密用法2013年12月11日 11:50:35 iteye_751 阅读数:428--加密字符串create or replace function des3_enc( i ...