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参数值”,可是这种格式只能传递简单的键值对信息,不能传递结构化数据,也无法传递数组形式的参数,因而它不适用 ...
随机推荐
- HCIE笔记-第七节-ICMP+ARP
ICMP重定向 作用:解决网络中的次优路径 触发:当某一个设备收到一个数据,进行转发时发现还要从该接口进行转发,于是触发ICMP重定向. 报文:Type=5,Code=0 ARP -- 地址解析协议 ...
- Java语言学习day05--7月2日
今日内容介绍1.引用类型变量的创建及使用2.流程控制语句之选择语句 ###01创建引用类型变量公式 * A: 创建引用类型变量公式 * a: 我们要学的Scanner类是属于引用数据类型,我们先了解下 ...
- 2021.11.11 P4052 [JSOI2007]文本生成器(AC自动机+DP)
2021.11.11 P4052 [JSOI2007]文本生成器(AC自动机+DP) https://www.luogu.com.cn/problem/P4052 题意: JSOI 交给队员 ZYX ...
- 原生 js 重点案例 [tab栏切换]
代码示例 : <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- Java中日期格式化的实现算法
package com.study.test; import java.io.Serializable; import java.text.SimpleDateFormat; import java. ...
- Book2Notion:将豆瓣图书信息同步到Notion的Chrome插件
背景 前几天写了一个python脚本从豆瓣爬数据然后保存到Notion,被身边同学吐槽使用起来太麻烦,而且也不是所有人都会Python(原话是充满了码农版"何不食肉糜").正好最近 ...
- 关于fiddler抓包一键生成python脚本
本人贡献一篇关于抓包转换成脚本的文章 步骤一 打开fiddler,抓到包之后,保存成txt文件 步骤二 脚本里str_filename改成保存的文件名 步骤三 执行脚本一键转换 附上脚本,感谢关注~ ...
- OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "ip": executable file not found in $PATH: unknown (Docker容器没有ip addr命令:exec ip addr 报错)
一.报错 1.报错信息1: OCI runtime exec failed: exec failed: container_linux.go:380: starting container proce ...
- 2022 Java生态系统报告:Java 11超Java 8、Oracle在缩水、Amazon在崛起!
近日,New Relic发布了最新的2022 Java生态系统报告,这份报告可以帮助我们深入的了解Java体系的最新使用情况,下面就一起来看看2022年,Java发展的怎么样了,还是Java 8 YY ...
- tensorflwo-gpu win10_64bit 的安装版本问题
tensorflow 1.3 配 cuda8.0 + cudnn5.1tensorflow 1.4 配 cuda8.0 + cudnn6.0 有没有更大的字体???我要配!!!!!