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参数值”,可是这种格式只能传递简单的键值对信息,不能传递结构化数据,也无法传递数组形式的参数,因而它不适用 ...
随机推荐
- 2021.12.07 P4291 [HAOI2008]排名系统(Treap)
2021.12.07 P4291 [HAOI2008]排名系统(Treap) https://www.luogu.com.cn/problem/P4291 双倍经验: https://www.luog ...
- transform动画
1. html 结构 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- Jmeter监控平台搭建:JMeter+InfluxDB+Grafana
背景 平时一般用Jmeter的Gui模式,添加对应的插件,查看每秒线程数.TPS.响应时间等曲线,其实高并发是不建议这么看的. 解决方案 可以搭配InfluxDB+Grafana工具,使Jmeter异 ...
- 3.2 常用Linux命令
1.ifconfig命令 ifconfig命令用于获取网卡配置与网络状态等信息,英文全称为"interface config",语法格式为"ifconfig [参数] [ ...
- mybatis混淆概念
1.resultMap与resultType <mapper namespace="com.dao.FilmMapper"> <resultMap id=&quo ...
- Linux网络流量相关
一直以来对Linux网络这块都感觉比较乱 遇到一个UDP丢包的问题:在测试中,一台VM虚拟机,CPU利用率55%左右,内存利用率7%左右,网卡流量也远没到限制的时候出现了丢包情况 使用netstat ...
- Python学习之路——类-面向对象编程
类 面向对象编程 通过类获取一个对象的过程 - 实例化 类名()会自动调用类中的__init__方法 类和对象之间的关系? 类 是一个大范围 是一个模子 它约束了事务有哪些属性 但是不能约束具体的值 ...
- 树莓派开发笔记(十三):入手研华ADVANTECH工控树莓派UNO-220套件(二):安装rtc等驱动
前言 前面运行了系统,本篇是安装对应套装的驱动,使rtc等外设生效,树莓派本身是不带rtc外设的. UNO-220-P4N1AE 驱动下载 官方下载:https://www.advan ...
- 干货|SQL语句大全,所有的SQL都在这里了(建议收藏)
一个执着于技术的公众号 一.基础 1.登录数据库 mysql -uroot -p123123 2.创建数据库 create database <数据库名> 3.删除数据库 drop dat ...
- java.sql和javax.sql的区别
根据 JDBC 规范,javax.sql 包中的类和接口首先作为 JDBC 2.0 可选包提供.此可选程序包以前与 J2SE1.2 中的 java.sql 程序包是分开的.从 J2SE1.4 开始,这 ...