Json多层级动态结构数据解析
一、工具
- 标准化 - Gson是一个由Google管理的标准化库。
- 高效 - 这是对Java标准库的可靠,快速和高效的扩展。
- 优化 - Gson库经过高度优化。
- 支持泛型 - 它为泛型提供了广泛的支持。
- 支持复杂的内部类 - 它支持具有深度继承层次结构的复杂对象。
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
三、工具类
public class JsonUtils {
/**
* 将指定JSON转为Map对象,Key固定为String,对应JSONkey
* Value分情况:
* 1. Value是字符串,自动转为字符串,例如:{"a","b"} 6 * 2. Value是其他JSON对象,自动转为Map,例如:{"a":{"b":"2"}}}
* 3. Value是数组,自动转为List<Map>,例如:{"a":[{"b":"2"},"c":"3"]}
* @param json 输入的JSON对象
* @return 动态的Map集合
*/
public static Map<String,Object> transferToMap(String json) {
Gson gson = new Gson();
Map<String, Object> map = gson.fromJson(json,
new TypeToken<Map<String, Object>>() {}.getType());
return map;
} /**
* 简化方法
* @param json 原始的JSON数据
* @param path OGNL规则表达式
* @param clazz Value对应的目标类
* @return clazz对应数据
*/
public static <T> T getValue(String json, String path, Class<T> clazz) {
try {
Map map = transferToMap(json);
OgnlContext context = new OgnlContext();
context.setRoot(map);
T value = (T) Ognl.getValue(path, context, context.getRoot());
return value;
} catch (Exception e) {
throw new RuntimeException(e);
}
} public static <T> T getValueFromMap(Map map, String path, Class<T> clazz) {
try {
OgnlContext context = new OgnlContext();
context.setRoot(map);
T value = (T) Ognl.getValue(path, context, context.getRoot());
return value;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
四、测试用例
import com.itlaoqi.JsonUtils;
import org.junit.Test;
import java.util.List;
import java.util.Map;
public class JsonCase {
/**
* {
* "a": {
* "b": {
* "c": {
* "d": {
* "e": "nothing"
* }
* }
* }
* }
* }
*/
/**
* 超多层级JSON嵌套的快速提取
*/
@Test
public void case0(){
String text = "{\n" +
" \"a\": {\n" +
" \"b\": {\n" +
" \"c\": {\n" +
" \"d\": {\n" +
" \"e\": \"nothing\"\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
Map<String, Object> jsonMap = JsonUtils.transferToMap(json);
String e = JsonUtils.getValue(text, "a.b.c.d.e", String.class);
System.out.println(e);
}
/**
* {
* "showapi_res_error": "",
* "showapi_res_id": "628cc9850de3769f06edbb49",
* "showapi_res_code": 0,
* "showapi_fee_num": 1,
* "showapi_res_body": {"ret_code":0,"area":"南 安","areaid":"101230506","areaCode":"350583","hourList":
[{"weather_code":"07","time":"202205242000","area":"南安","wind_direction":"东南
风","wind_power":"0-3级 微风 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"20"},
{"weather_code":"07","time":"202205242100","area":"南安","wind_direction":"东 风","wind_power":"0-3级 微风 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"20"},
{"weather_code":"07","time":"202205242200","area":"南安","wind_direction":"东 风","wind_power":"0-3级 微风 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"20"},
{"weather_code":"07","time":"202205242300","area":"南安","wind_direction":"东 风","wind_power":"0-3级 微风 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"20"},
{"weather_code":"07","time":"202205250000","area":"南安","wind_direction":"南 风","wind_power":"0-3级 微风 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"21"},
{"weather_code":"07","time":"202205250100","area":"南安","wind_direction":"西 风","wind_power":"0-3级 微风 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"21"},
{"weather_code":"07","time":"202205250200","area":"南安","wind_direction":"西北
风","wind_power":"0-3级 微风 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"21"},
{"weather_code":"02","time":"202205250300","area":"南安","wind_direction":"西北
风","wind_power":"0-3级 微风
<5.4m/s","weather":"阴","areaid":"101230506","temperature":"21"},
{"weather_code":"02","time":"202205250400","area":"南安","wind_direction":"西北
风","wind_power":"0-3级 微风
<5.4m/s","weather":"阴","areaid":"101230506","temperature":"21"},
{"weather_code":"02","time":"202205250500","area":"南安","wind_direction":"西 风","wind_power":"0-3级 微风
<5.4m/s","weather":"阴","areaid":"101230506","temperature":"21"},
{"weather_code":"07","time":"202205250600","area":"南安","wind_direction":"西北
风","wind_power":"0-3级 微风 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"21"},
{"weather_code":"07","time":"202205250700","area":"南安","wind_direction":"西北
风","wind_power":"0-3级 微风 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"21"},
{"weather_code":"07","time":"202205250800","area":"南安","wind_direction":"西北
风","wind_power":"0-3级 微风 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"21"},
{"weather_code":"07","time":"202205250900","area":"南安","wind_direction":"西南
风","wind_power":"0-3级 微风 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"22"},
{"weather_code":"07","time":"202205251000","area":"南安","wind_direction":"南 风","wind_power":"0-3级 微风 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"23"},
{"weather_code":"07","time":"202205251100","area":"南安","wind_direction":"东 风","wind_power":"0-3级 微风 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"24"},
{"weather_code":"07","time":"202205251200","area":"南安","wind_direction":"东 风","wind_power":"0-3级 微风 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"24"},
{"weather_code":"07","time":"202205251300","area":"南安","wind_direction":"东 风","wind_power":"0-3级 微风 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"25"},
{"weather_code":"07","time":"202205251400","area":"南安","wind_direction":"东南
风","wind_power":"0-3级 微风 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"25"},
{"weather_code":"07","time":"202205251500","area":"南安","wind_direction":"东南
风","wind_power":"0-3级 微风 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"25"},
{"weather_code":"07","time":"202205251600","area":"南安","wind_direction":"东南
风","wind_power":"0-3级 微风 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"24"},
{"weather_code":"07","time":"202205251700","area":"南安","wind_direction":"东南
风","wind_power":"0-3级 微风 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"23"},
{"weather_code":"07","time":"202205251800","area":"南安","wind_direction":"东南
风","wind_power":"0-3级 微风 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"23"},
{"weather_code":"07","time":"202205251900","area":"南安","wind_direction":"东南
风","wind_power":"0-3级 微风 <5.4m/s","weather":"小 雨","areaid":"101230506","temperature":"23"}]}
* }
*/
private String json = "{\n" +
"\"showapi_res_error\": \"\",\n" +
"\"showapi_res_id\": \"628cc9850de3769f06edbb49\",\n" +
"\"showapi_res_code\": 0,\n" +
"\"showapi_fee_num\": 1,\n" +
"\"showapi_res_body\": {\"ret_code\":0,\"area\":\"南安
\",\"areaid\":\"101230506\",\"areaCode\":\"350583\",\"hourList\":
[{\"weather_code\":\"07\",\"time\":\"202205242000\",\"area\":\"南安
\",\"wind_direction\":\"东南风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"小雨
\",\"areaid\":\"101230506\",\"temperature\":\"20\"},
{\"weather_code\":\"07\",\"time\":\"202205242100\",\"area\":\"南安
\",\"wind_direction\":\"东风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"小雨
\",\"areaid\":\"101230506\",\"temperature\":\"20\"},
{\"weather_code\":\"07\",\"time\":\"202205242200\",\"area\":\"南安
\",\"wind_direction\":\"东风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"小雨
\",\"areaid\":\"101230506\",\"temperature\":\"20\"},
{\"weather_code\":\"07\",\"time\":\"202205242300\",\"area\":\"南安
\",\"wind_direction\":\"东风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"小雨
\",\"areaid\":\"101230506\",\"temperature\":\"20\"},
{\"weather_code\":\"07\",\"time\":\"202205250000\",\"area\":\"南安
\",\"wind_direction\":\"南风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"小雨
\",\"areaid\":\"101230506\",\"temperature\":\"21\"},
{\"weather_code\":\"07\",\"time\":\"202205250100\",\"area\":\"南安
\",\"wind_direction\":\"西风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"小雨
\",\"areaid\":\"101230506\",\"temperature\":\"21\"},
{\"weather_code\":\"07\",\"time\":\"202205250200\",\"area\":\"南安
\",\"wind_direction\":\"西北风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"小雨
\",\"areaid\":\"101230506\",\"temperature\":\"21\"},
{\"weather_code\":\"02\",\"time\":\"202205250300\",\"area\":\"南安
\",\"wind_direction\":\"西北风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"阴
\",\"areaid\":\"101230506\",\"temperature\":\"21\"},
{\"weather_code\":\"02\",\"time\":\"202205250400\",\"area\":\"南安
\",\"wind_direction\":\"西北风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"阴
\",\"areaid\":\"101230506\",\"temperature\":\"21\"},
{\"weather_code\":\"02\",\"time\":\"202205250500\",\"area\":\"南安
\",\"wind_direction\":\"西风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"阴
\",\"areaid\":\"101230506\",\"temperature\":\"21\"},
{\"weather_code\":\"07\",\"time\":\"202205250600\",\"area\":\"南安
\",\"wind_direction\":\"西北风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"小雨
\",\"areaid\":\"101230506\",\"temperature\":\"21\"},
{\"weather_code\":\"07\",\"time\":\"202205250700\",\"area\":\"南安
\",\"wind_direction\":\"西北风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"小雨
\",\"areaid\":\"101230506\",\"temperature\":\"21\"},
{\"weather_code\":\"07\",\"time\":\"202205250800\",\"area\":\"南安
\",\"wind_direction\":\"西北风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"小雨
\",\"areaid\":\"101230506\",\"temperature\":\"21\"},
{\"weather_code\":\"07\",\"time\":\"202205250900\",\"area\":\"南安
\",\"wind_direction\":\"西南风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"小雨
\",\"areaid\":\"101230506\",\"temperature\":\"22\"},
{\"weather_code\":\"07\",\"time\":\"202205251000\",\"area\":\"南安
\",\"wind_direction\":\"南风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"小雨
\",\"areaid\":\"101230506\",\"temperature\":\"23\"},
{\"weather_code\":\"07\",\"time\":\"202205251100\",\"area\":\"南安
\",\"wind_direction\":\"东风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"小雨
\",\"areaid\":\"101230506\",\"temperature\":\"24\"},
{\"weather_code\":\"07\",\"time\":\"202205251200\",\"area\":\"南安
\",\"wind_direction\":\"东风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"小雨
\",\"areaid\":\"101230506\",\"temperature\":\"24\"},
{\"weather_code\":\"07\",\"time\":\"202205251300\",\"area\":\"南安
\",\"wind_direction\":\"东风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"小雨
\",\"areaid\":\"101230506\",\"temperature\":\"25\"},
{\"weather_code\":\"07\",\"time\":\"202205251400\",\"area\":\"南安
\",\"wind_direction\":\"东南风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"小雨
\",\"areaid\":\"101230506\",\"temperature\":\"25\"},
{\"weather_code\":\"07\",\"time\":\"202205251500\",\"area\":\"南安
\",\"wind_direction\":\"东南风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"小雨
\",\"areaid\":\"101230506\",\"temperature\":\"25\"},
{\"weather_code\":\"07\",\"time\":\"202205251600\",\"area\":\"南安
\",\"wind_direction\":\"东南风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"小雨
\",\"areaid\":\"101230506\",\"temperature\":\"24\"},
{\"weather_code\":\"07\",\"time\":\"202205251700\",\"area\":\"南安
\",\"wind_direction\":\"东南风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"小雨
\",\"areaid\":\"101230506\",\"temperature\":\"23\"},
{\"weather_code\":\"07\",\"time\":\"202205251800\",\"area\":\"南安
\",\"wind_direction\":\"东南风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"小雨
\",\"areaid\":\"101230506\",\"temperature\":\"23\"},
{\"weather_code\":\"07\",\"time\":\"202205251900\",\"area\":\"南安
\",\"wind_direction\":\"东南风\",\"wind_power\":\"0-3级 微风 <5.4m/s\",\"weather\":\"小雨
\",\"areaid\":\"101230506\",\"temperature\":\"23\"}]}\n" +
"}";
//将JSON转为标准Map结构
@Test
public void case1(){
Map<String, Object> jsonMap = JsonUtils.transferToMap(json);
System.out.println(jsonMap);
}
/**
* OGNL直接提取数据,Value为子JSON对象的情况
*/
@Test
public void case2(){
Map<String, Object> jsonMap = JsonUtils.transferToMap(json);
Map resBody = JsonUtils.getValue(json, "showapi_res_body", Map.class);
System.out.println(resBody);
}
/**
* OGNL直接提取数据,Value为标准字符串的情况
*/
@Test
public void case3(){
Map<String, Object> jsonMap = JsonUtils.transferToMap(json);
String value = JsonUtils.getValue(json, "showapi_res_body.area", String.class);
System.out.println(value);
}
/**
* OGNL直接提取数据,Value为标准字符串的情况,Value为数组的情况
*/
@Test
public void case4(){
Map<String, Object> jsonMap = JsonUtils.transferToMap(json);
List<Map> hourList = JsonUtils.getValue(json, "showapi_res_body.hourList",st.class);
System.out.println(hourList);
// 每一个集合对象都是List
for(Map hour : hourList){
System.out.println(hour);
}
}
/**
* 利用List语法获取第6个时点天气预报
*/
@Test
public void case5(){
Map<String, Object> jsonMap = JsonUtils.transferToMap(json);
String area = JsonUtils.getValue(json,"showapi_res_body.hourList[5].weather_code", String.class);
System.out.println(area);
}
}
Json多层级动态结构数据解析的更多相关文章
- javascript如何解析json对javascript如何解析json对象并动态赋值到select列表象并动态赋值到select列表
原文 javascript如何解析json对象并动态赋值到select列表 JSON(JavaScriptObject Notation)一种简单的数据格式,比xml更轻巧.JSON是JavaScri ...
- json.net(Json.NET - Newtonsoft)利用动态类解析json字符串
将对象转换为字符串很简单,唯一要注意的点就是为了避免循环要在需要的字段上添加jsonignore属性.可以参照这篇博文:http://www.mamicode.com/info-detail-1456 ...
- 纯js遍历json获取值动态为select添加option
遍历json数组 并动态为select添加option 直接上代码,重要部分有注解 <!DOCTYPE html> <html lang="en"> < ...
- js中解析json对象:JSON.parse()用于从一个字符串中解析出json对象, JSON.stringify()用于从一个对象解析出字符串。
JSON.parse()用于从一个字符串中解析出json对象. var str = '{"name":"huangxiaojian","age&quo ...
- javascript里面的数组,json对象,动态添加,修改,删除示例
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- JSON的简单使用_解析前台传来的JSON数据
package cn.rocker.json; import org.junit.Test; import net.sf.json.JSONArray; import net.sf.json.JSON ...
- JSON数据的生成与解析
JSON数据的生成与解析.首先先到网上下载一个json jar包,我用的是org.json 演示样例代码: package json; import org.json.JSONArray; impor ...
- django基础之day08,分页器从无到有,动态思路解析全过程
*********分页器从无到有的全过程,动态思路解析如下:******** 1.通过book_queryset = models.Book.objects.all()[start_num:end_n ...
- Java开发笔记(一百零八)JSON串的定义和解析
前面提到URL尾巴支持添加请求参数,具体格式形如“参数A名称=A参数值&参数B名称=B参数值”,可是这种格式只能传递简单的键值对信息,不能传递结构化数据,也无法传递数组形式的参数,因而它不适用 ...
随机推荐
- jsp第六次作业
1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8&quo ...
- 技术管理进阶——一线Leader怎么做?经理的速成宝典
原创不易,求分享.求一键三连 本期培训材料关注公众号后回复:经理培训,获得 前段时间有个同学问我有没有一线Leader的速成培训课程,很好的问题,首先我们需要定义一下什么是小Leader: 所谓小Le ...
- 通过OptaPlanner优化 COVID-19 疫苗接种预约安排(2)
本文为OptaPlanner官方博客<Optimizing COVID-19 vaccination appointment scheduling>的第二篇译文.第一篇介绍了通过OptaP ...
- centos下安装ansible自动化工具(超详细,包含基本使用)
ansible官网:https://www.ansible.com 众所周知,ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef ...
- springmvc-02(配置版与注解版区别)
首先,我们来看配置版和注解版的相同步骤: 1.新建一个Moudle , springmvc-02-hello , 添加web的支持! 2.确定导入了SpringMVC 的依赖! 3.配置web.xml ...
- js console.log打印变量注意事项
如果是基本类型变量是没有异常的 let str = 'string' console.log(str) // string str = '改变了str变量' 如果是引用类型,打印就要注意了 let o ...
- 【ACM程序设计】求短路 Floyd算法
最短路 floyd算法 floyd是一个基于贪心思维和动态规划思维的计算所有点到所有点的最短距离的算法. P57-图-8.Floyd算法_哔哩哔哩_bilibili 对于每个顶点v,和任一顶点对(i, ...
- 附011.常见Linux镜像站点大全
开源系统镜像站点 国内Mirrors站点 企业类站点 阿里巴巴开源Mirrors站点:https://developer.aliyun.com/mirror/ 腾讯开源Mirrors站点:https: ...
- filter/backdrop-filter 毛玻璃效果
对于方式二采用的方式,如果存在边缘模糊程度不够,可以设置扩大伪元素范围(margin: -20px),父元素超出裁剪(overflow: hidden). <!DOCTYPE html> ...
- KMP算法学习以及小结(好马不吃回头草系列)
首先请允许我对KMP算法的三位创始人Knuth,Morris,Pratt致敬,这三位优秀的算法科学家发明的这种匹配模式可以大大避免重复遍历的情况,从而使得字符串的匹配的速度更快,效率更高. 首先引入对 ...