一、工具

(1)GSON
Google Gson是一个简单的基于Java的库,用于将Java对象序列化为JSON,反之亦然。 它是由Google开发的一个开源库。
以下几点说明为什么应该使用这个库 -
  • 标准化 - Gson是一个由Google管理的标准化库。
  • 高效 - 这是对Java标准库的可靠,快速和高效的扩展。
  • 优化 - Gson库经过高度优化。
  • 支持泛型 - 它为泛型提供了广泛的支持。
  • 支持复杂的内部类 - 它支持具有深度继承层次结构的复杂对象。
(2)OGNL表达式
OGNL表达式是Object-Graph Navigation Language的缩写,是一种功能强大的表达式语言,通过
简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,实现字段类型转换。
二、mavan所需引入的依赖
  1. <dependency>
  2. <groupId>ognl</groupId>
  3. <artifactId>ognl</artifactId>
  4. <version>3.1.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
  8. <version>2.9.0</version>
  9. </dependency>
  10. <dependency>
  11. <groupId>junit</groupId>
  12. <artifactId>junit</artifactId>
  13. <version>4.13.2</version>
  14. </dependency>

三、工具类

  1. public class JsonUtils {

    /**
  2. * 将指定JSON转为Map对象,Key固定为String,对应JSONkey
  3. * Value分情况:
  4. * 1. Value是字符串,自动转为字符串,例如:{"a","b"} 6 * 2. Value是其他JSON对象,自动转为Map,例如:{"a":{"b":"2"}}}
  5. * 3. Value是数组,自动转为List<Map>,例如:{"a":[{"b":"2"},"c":"3"]}
  6. * @param json 输入的JSON对象
  7. * @return 动态的Map集合
  8. */
  9. public static Map<String,Object> transferToMap(String json) {
  10. Gson gson = new Gson();
  11. Map<String, Object> map = gson.fromJson(json,
  12. new TypeToken<Map<String, Object>>() {}.getType());
  13. return map;
  14. }
  15.  
  16. /**
  17. * 简化方法
  18. * @param json 原始的JSON数据
  19. * @param path OGNL规则表达式
  20. * @param clazz Value对应的目标类
  21. * @return clazz对应数据
  22. */
  23. public static <T> T getValue(String json, String path, Class<T> clazz) {
  24. try {
  25. Map map = transferToMap(json);
  26. OgnlContext context = new OgnlContext();
  27. context.setRoot(map);
  28. T value = (T) Ognl.getValue(path, context, context.getRoot());
  29. return value;
  30. } catch (Exception e) {
  31. throw new RuntimeException(e);
  32. }
  33. }
  34.  
  35. public static <T> T getValueFromMap(Map map, String path, Class<T> clazz) {
  36. try {
  37. OgnlContext context = new OgnlContext();
  38. context.setRoot(map);
  39. T value = (T) Ognl.getValue(path, context, context.getRoot());
  40. return value;
  41. } catch (Exception e) {
  42. throw new RuntimeException(e);
  43. }
  44. }
  45. }

四、测试用例

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多层级动态结构数据解析的更多相关文章

  1. javascript如何解析json对javascript如何解析json对象并动态赋值到select列表象并动态赋值到select列表

    原文 javascript如何解析json对象并动态赋值到select列表 JSON(JavaScriptObject Notation)一种简单的数据格式,比xml更轻巧.JSON是JavaScri ...

  2. json.net(Json.NET - Newtonsoft)利用动态类解析json字符串

    将对象转换为字符串很简单,唯一要注意的点就是为了避免循环要在需要的字段上添加jsonignore属性.可以参照这篇博文:http://www.mamicode.com/info-detail-1456 ...

  3. 纯js遍历json获取值动态为select添加option

    遍历json数组 并动态为select添加option 直接上代码,重要部分有注解 <!DOCTYPE html> <html lang="en"> < ...

  4. js中解析json对象:JSON.parse()用于从一个字符串中解析出json对象, JSON.stringify()用于从一个对象解析出字符串。

    JSON.parse()用于从一个字符串中解析出json对象. var str = '{"name":"huangxiaojian","age&quo ...

  5. javascript里面的数组,json对象,动态添加,修改,删除示例

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  6. JSON的简单使用_解析前台传来的JSON数据

    package cn.rocker.json; import org.junit.Test; import net.sf.json.JSONArray; import net.sf.json.JSON ...

  7. JSON数据的生成与解析

    JSON数据的生成与解析.首先先到网上下载一个json jar包,我用的是org.json 演示样例代码: package json; import org.json.JSONArray; impor ...

  8. django基础之day08,分页器从无到有,动态思路解析全过程

    *********分页器从无到有的全过程,动态思路解析如下:******** 1.通过book_queryset = models.Book.objects.all()[start_num:end_n ...

  9. Java开发笔记(一百零八)JSON串的定义和解析

    前面提到URL尾巴支持添加请求参数,具体格式形如“参数A名称=A参数值&参数B名称=B参数值”,可是这种格式只能传递简单的键值对信息,不能传递结构化数据,也无法传递数组形式的参数,因而它不适用 ...

随机推荐

  1. python3-认识内置对象,运算符,表达式

    1 . 认识内置对象 在python中一切皆对象, 整数,实数,复数,字符串,列表,元组,字典,集合,zip,  map, enumerate, filter , 函数 ,类 , 分类:内置对象,标准 ...

  2. 联邦平均算法(Federated Averaging Algorithm,FedAvg)

    设一共有\(K\)个客户机, 中心服务器初始化模型参数,执行若干轮(round),每轮选取至少1个至多\(K\)个客户机参与训练,接下来每个被选中的客户机同时在自己的本地根据服务器下发的本轮(\(t\ ...

  3. Apache Doris ODBC外表之Postgresql使用指南

    Apache Doris 社区 2022 年的总体规划,包括待开展或已开展.以及已完成但需要持续优化的功能.文档.社区建设等多方面,我们期待有更多的小伙伴参与进来讨论.同时也希望多多关注Doris,给 ...

  4. 使用 Prometheus Alertmanager 模块发送 Doris 异常信息至钉钉报警群

    基础环境 1.Prometheus 版本:2.22.2 下载地址: https://github.com/prometheus/prometheus/releases/download/v2.22.2 ...

  5. XCTF练习题---MISC---适合作为桌面

    XCTF练习题---适合作为桌面 flag:flag{38a57032085441e7} 解题步骤: 1.观察题目,下载附件 2.拿到题目以后是一张图片,切换一下通道,发现一张二维码,使用QR进行翻译 ...

  6. [RPC学习]Dubbo+nacos实现动态更新内存RTree

    1.背景 服务架构一般都是从 单体架构 -> 微服务架构 -> 分布式架构 的迭代,我上一家公司就是在业务发展到一定规模时,开始拆老的单体服务,按业务维度拆成多个微服务,服务之间用的是HT ...

  7. 使用allatori混淆代码

    引言 为了保密需要,我们开发出来的程序在实际部署之前,需要先将jar包的代码进行混淆,让用户使用反编译也无法获得源码. allatori就是这样一个混淆java jar包代码的工具. 操作步骤 Ste ...

  8. SpringJdbcTemplate简单实现

    SpringJdbcTemplate 配置文件 1.依赖坐标 <dependencies> <dependency> <groupId>javax.servlet& ...

  9. Mysql limit 优化优化

    MySql 性能到底能有多高?用了php半年多,真正如此深入的去思考这个问题还是从前天开始.有过痛苦有过绝望,到现在充满信心! MySql 这个数据库绝对是适合dba级的高手去玩的,一般做一点1万篇新 ...

  10. jmeter 基础使用

    相关入门链接 JMeter 5.4.1 教程 插件安装 并发线程 ServerAgent 服务器监控 ServerAgent 下载 Ubuntu 20.04 install jdk/jre 服务器监控 ...