Java中JSON有哪些深入浅出的使用技巧和方式?

2026-04-19 17:012阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计719个文字,预计阅读时间需要3分钟。

Java中JSON有哪些深入浅出的使用技巧和方式?

1. JSON对象转换 - 将JSONObject转换为JSON字符串 java JSONObject jsonObject=new JSONObject(); jsonObject.put(name, test); String str=JSONObject.toJSONString(jsonObject); String str={\name\:\test\}; JSONObject json=JSONObject.fromObject(str);

1. 常用的JSON转换

JSONObject 转 JSON 字符串

JSONObject json = new JSONObject(); jsonObject.put("name", "test"); String str = JSONObject.toJSONString(json);

JSON字符串转JSON

String str = "{\"name\":\"test\"}"; JSONObject json = JSONObject.parseObject(str);

实体类转JSON

Test test = new Test(); test.setName("test"); String testStr = JSONObject.toJSONString(test); JSONObject json = JSONObject.parseObject(testStr);

Map转JSON

JSONObject json = JSONObject.parseObject(JSON.toJSONString(map));

JSON转Map

Map jsonToMap = JSONObject.parseObject(jsonObject.toJSONString());

2. 将多个JSON合并一个

JSONObject totalJSON = new JSONObject(); totalJSON.putAll(json1); totalJSON.putAll(json2);

json1,json2 为JSONObject。 最终的代码格式:

{ json1:{}, json2:{} }

3.JSON拆分

不同的需求有不同的做法,以下提供两种解决思路

  • 定义两个或多个JSON进行put和remove 比如明确需要哪些字段的时候可以定义一个数组用来存放key信息。存放和删除的时候只需要遍历数组就可以。
  • 遍历JSON,获取key,value再重新put

4.JSON遍历

定义一个工具类,获取key和value

Java中JSON有哪些深入浅出的使用技巧和方式?

if(object instanceof JSONObject) { JSONObject jsonObject = (JSONObject) object; for (Map.Entry<String, Object> entry: jsonObject.entrySet()) { Object o = entry.getValue(); if(o instanceof String) { System.out.println("key:" + entry.getKey() + ",value:" + entry.getValue()); } else { jsonLoop(o); } } } if(object instanceof JSONArray) { JSONArray jsonArray = (JSONArray) object; for(int i = 0; i < jsonArray.size(); i ++) { jsonLoop(jsonArray.get(i)); } }

JSONArray遍历的方式有很多种

for

for(int i = 0; i < jsonArray.size(); i++){ JSONObject json = jsonArray.getJSONObject(i); }

foreach

jsonArray.forEach(o -> { if (o instanceof JSONObject) { JSONObject json = (JSONObject) o; }

Iterator

JSONObject jsonObject = new JSONObject(jsonString); Iterator iterator = jsonObject.keys(); while(iterator.hasNext()){ key = (String) iterator.next(); value = jsonObject.getString(key); }

5.JSONPath

另外向大家推荐一个非常好用的工具:JSONPath。

JSONPath是一种简单的方法来提取给定JSON的部分内容,使用方式类似于正则表达式。 GitHub地址: github.com/json-path/JsonPath

简单描述下使用方法已经自己使用的案例 pom文件依赖:

<dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> <version>2.2.0</version> </dependency>

JsonPath表达式总是以与XPath表达式结合使用XML文档相同的方式引用JSON结构。

JsonPath中的“根成员对象”始终称为$,无论是对象还是数组。

JsonPath表达式可以使用点表示法。

这里有个表格,说明JSONPath语法元素和对应XPath元素的对比。

官方案例:

详细大家还是参照官方解说。 下面是我写的案例:

JSONArray jsonArray = JSONPath.read("$.ePrint.common..label");

需要注意的是这里的JSONArray是JSONPath的,所以导包是:net.minidev.json.JSONPath JSON格式不会变,所以可以转换为alibaba的JSONArray:

com.alibaba.fastjson.JSONArray jsonArr = JSON.parse(jsonArray.toString());

这里要注意一点也是我踩过的坑:如果获取一个JSONObject下有多个同名的JSONArray,那么返回的[]也是多个。要先遍历获取到的数据,在取其中的一个JSON块。

到此这篇关于浅谈在Java中JSON的多种使用方式的文章就介绍到这了,更多相关Java中JSON使用方式内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计719个文字,预计阅读时间需要3分钟。

Java中JSON有哪些深入浅出的使用技巧和方式?

1. JSON对象转换 - 将JSONObject转换为JSON字符串 java JSONObject jsonObject=new JSONObject(); jsonObject.put(name, test); String str=JSONObject.toJSONString(jsonObject); String str={\name\:\test\}; JSONObject json=JSONObject.fromObject(str);

1. 常用的JSON转换

JSONObject 转 JSON 字符串

JSONObject json = new JSONObject(); jsonObject.put("name", "test"); String str = JSONObject.toJSONString(json);

JSON字符串转JSON

String str = "{\"name\":\"test\"}"; JSONObject json = JSONObject.parseObject(str);

实体类转JSON

Test test = new Test(); test.setName("test"); String testStr = JSONObject.toJSONString(test); JSONObject json = JSONObject.parseObject(testStr);

Map转JSON

JSONObject json = JSONObject.parseObject(JSON.toJSONString(map));

JSON转Map

Map jsonToMap = JSONObject.parseObject(jsonObject.toJSONString());

2. 将多个JSON合并一个

JSONObject totalJSON = new JSONObject(); totalJSON.putAll(json1); totalJSON.putAll(json2);

json1,json2 为JSONObject。 最终的代码格式:

{ json1:{}, json2:{} }

3.JSON拆分

不同的需求有不同的做法,以下提供两种解决思路

  • 定义两个或多个JSON进行put和remove 比如明确需要哪些字段的时候可以定义一个数组用来存放key信息。存放和删除的时候只需要遍历数组就可以。
  • 遍历JSON,获取key,value再重新put

4.JSON遍历

定义一个工具类,获取key和value

Java中JSON有哪些深入浅出的使用技巧和方式?

if(object instanceof JSONObject) { JSONObject jsonObject = (JSONObject) object; for (Map.Entry<String, Object> entry: jsonObject.entrySet()) { Object o = entry.getValue(); if(o instanceof String) { System.out.println("key:" + entry.getKey() + ",value:" + entry.getValue()); } else { jsonLoop(o); } } } if(object instanceof JSONArray) { JSONArray jsonArray = (JSONArray) object; for(int i = 0; i < jsonArray.size(); i ++) { jsonLoop(jsonArray.get(i)); } }

JSONArray遍历的方式有很多种

for

for(int i = 0; i < jsonArray.size(); i++){ JSONObject json = jsonArray.getJSONObject(i); }

foreach

jsonArray.forEach(o -> { if (o instanceof JSONObject) { JSONObject json = (JSONObject) o; }

Iterator

JSONObject jsonObject = new JSONObject(jsonString); Iterator iterator = jsonObject.keys(); while(iterator.hasNext()){ key = (String) iterator.next(); value = jsonObject.getString(key); }

5.JSONPath

另外向大家推荐一个非常好用的工具:JSONPath。

JSONPath是一种简单的方法来提取给定JSON的部分内容,使用方式类似于正则表达式。 GitHub地址: github.com/json-path/JsonPath

简单描述下使用方法已经自己使用的案例 pom文件依赖:

<dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> <version>2.2.0</version> </dependency>

JsonPath表达式总是以与XPath表达式结合使用XML文档相同的方式引用JSON结构。

JsonPath中的“根成员对象”始终称为$,无论是对象还是数组。

JsonPath表达式可以使用点表示法。

这里有个表格,说明JSONPath语法元素和对应XPath元素的对比。

官方案例:

详细大家还是参照官方解说。 下面是我写的案例:

JSONArray jsonArray = JSONPath.read("$.ePrint.common..label");

需要注意的是这里的JSONArray是JSONPath的,所以导包是:net.minidev.json.JSONPath JSON格式不会变,所以可以转换为alibaba的JSONArray:

com.alibaba.fastjson.JSONArray jsonArr = JSON.parse(jsonArray.toString());

这里要注意一点也是我踩过的坑:如果获取一个JSONObject下有多个同名的JSONArray,那么返回的[]也是多个。要先遍历获取到的数据,在取其中的一个JSON块。

到此这篇关于浅谈在Java中JSON的多种使用方式的文章就介绍到这了,更多相关Java中JSON使用方式内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!