美文网首页
解析处理Josn的好帮手--JsonPath

解析处理Josn的好帮手--JsonPath

作者: 仁执 | 来源:发表于2019-02-12 20:41 被阅读0次

常见需求

1、读取字符串中某个key的值,但是这个key的层级比较深

2、筛选出json字符串中符合条件的叶子节点

3、读取某个key在json字符串中所有的值

4、替换json字符串中的某个key的值

5、移除json字符串中某个key

常用方法

1、map

2、map

3、map

4、set /put 

      其中set方法只能在xpath存在的情况下执行更新,put方法若指定的xpath 的key不存在,则添加,若存在,则执行更新操作

5、delete

使用示例

public static void main(String[] args) {

String jsonStr ="{\n" +

"    \"store\": {\n" +

"        \"book\": [\n" +

"            {\n" +

"                \"category\": \"reference\",\n" +

"                \"author\": \"hhhhh\",\n" +

"                \"title\": \"Sayings of the Century\",\n" +

"                \"price\": 8.95\n" +

"            }\n" +

"        ],\n" +

"        \"bicycle\": {\n" +

"            \"color\": \"red\",\n" +

"            \"price\": 19.95\n" +

"        }\n" +

"    },\n" +

"    \"expensive\": 10\n" +

"}";

DocumentContext json = JsonPath.parse(jsonStr);

String setMethodPath ="$..book[0].author";

String tagValue ="ReplacedText";

String putMethodPath ="$..book[0]";

String newValue ="hahahha";

String deleteMethodPath ="$.expensive";

System.out.println(json.set(setMethodPath, tagValue).jsonString());

System.out.println(json.put(putMethodPath,"newKey", newValue).jsonString());

System.out.println(json.put(putMethodPath,"author","updateValue").jsonString());

System.out.println(json.delete(deleteMethodPath).jsonString());

}

依赖包

<dependency>

<groupId>com.jayway.jsonpath</groupId>

<artifactId>json-path</artifactId>

<version>2.4.0</version>

</dependency>

<dependency>

<groupId>net.minidev</groupId>

<artifactId>minidev-parent</artifactId>

<version>2.3</version>

</dependency>

参考

https://github.com/jayway/JsonPath

文档

http://static.javadoc.io/com.jayway.jsonpath/json-path/2.2.0/com/jayway/jsonpath/WriteContext.html

相关文章

  • 解析处理Josn的好帮手--JsonPath

    常见需求 1、读取字符串中某个key的值,但是这个key的层级比较深 2、筛选出json字符串中符合条件的叶子节点...

  • jsonpath解析

    2019-06-03 jsonpath用来解析json数据使用的 python处理json格式用到的函数 impo...

  • josn的解析与序列化

    1.为什么要进行JOSN的解析与序列化 早期的Josn解析器基本上是使用JavaScript的eval()函数。由...

  • JSONPath入门及测试

    JSONPath入门 JSONPath - 是xpath在json的应用,是参照xpath表达式来解析XML文档的...

  • 07-数据提取-jsonpath

    jsonpath用来解析多层嵌套的json数据jsonpath官方文档 安装 语法 使用 字典的根节点为最外部大括...

  • JSONPath解析json

    JSONPath 用来解析多层嵌套的json数据,JsonPath 是一种信息抽取类库,是从JSON文档中抽取指定...

  • jsonpath模块01

    JSONPath 表达式 JSONPath 是参照,xpath表达式来解析xml文档的方式,json数据结构通常是...

  • AFNetworking使用Body传输json数据 和 jso

    一、关于josn解析价格相关的问题 1. json解析精度丢失 s: 3.0 Android : 3.0 I...

  • Android JOSN 解析

    一、JSON定义 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格...

  • Gson解析josn

    gson的Maven依赖:2019年9月5日11:23:12最新 gson解析json对象 对象参数: 解析代码 ...

网友评论

      本文标题:解析处理Josn的好帮手--JsonPath

      本文链接:https://www.haomeiwen.com/subject/esfleqtx.html