java json配置文件,如何使用java中的XPath / JsonPath更改json文件中的值

here is the json file

{

"session":

{

"name":"JSESSIONID",

"value":"5864FD56A1F84D5B0233E641B5D63B52"

},

"loginInfo":

{

"loginCount":77,

"previousLoginTime":"2014-12-02T11:11:58.561+0530"

}

}

I want to change the value of name.by directly giving XPath/JsonPath Like

($.session.name).changevalue("MYSESSINID")

this is just a Example

I am correctly using jackson library and using the below code for reading via XPath

ObjectMapper mapper = new ObjectMapper();

Object jsonObj=mapper.readValue(new File(Json file), Object.class);

Object name=PropertyUtils.getProperty(jsonObj, "session.name");

System.out.println("Name:"+name);

so is their a way to change the name by XPath

PropertyUtils.setProperty(jsonObj, "session.value", "new value");

still in the file its not working.

解决方案

private static final Configuration configuration = Configuration.builder()

.jsonProvider(new JacksonJsonNodeJsonProvider())

.mappingProvider(new JacksonMappingProvider())

.build();

@Test

public void a_value_can_be_updated(){

String originalJson = "{\n"

+ "\"session\":\n"

+ " {\n"

+ " \"name\":\"JSESSIONID\",\n"

+ " \"value\":\"5864FD56A1F84D5B0233E641B5D63B52\"\n"

+ " },\n"

+ "\"loginInfo\":\n"

+ " {\n"

+ " \"loginCount\":77,\n"

+ " \"previousLoginTime\":\"2014-12-02T11:11:58.561+0530\"\n"

+ " }\n"

+ "}";

JsonNode updatedJson = JsonPath.using(configuration).parse(originalJson).set("$.session.name", "MYSESSINID").json();

System.out.println(updatedJson.toString());

}

You can configure the default JsonProvider so you don't have to pass it in all calls.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于使用jsonPath解析json的问题,我可以回答你。jsonPath是一种用于查询Json数据的语言,类似于XPath用于xml,它可以快速定位到json数据特定的节点,从而方便进行操作。 在Java,我们可以使用JsonPath库来解析json数据。首先,需要添加jsonPath库的依赖,例如使用Maven的话,可以在pom.xml文件加入以下代码: ```xml <dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> <version>2.4.0</version> </dependency> ``` 然后,在Java代码,我们可以使用以下代码来解析json数据: ```java String json = "{\"name\":\"John\",\"age\":30,\"cars\":[ \"Ford\", \"BMW\", \"Fiat\" ]}"; JSONObject jsonObject = new JSONObject(json); // 先将json字符串转为JSONObject对象 String name = jsonObject.getString("name"); // 获取name字段的 int age = jsonObject.getInt("age"); // 获取age字段的 JSONArray cars = jsonObject.getJSONArray("cars"); // 获取cars字段的,这是一个JSONArray对象 String firstCar = cars.getString(0); // 获取cars数组第一个元素的 System.out.println(name); System.out.println(age); System.out.println(firstCar); ``` 此外,使用JsonPath库,我们也可以通过以下代码来获取json数据特定的节点: ```java String json = "{\"name\":\"John\",\"age\":30,\"cars\":[ \"Ford\", \"BMW\", \"Fiat\" ]}"; Object document = Configuration.defaultConfiguration().jsonProvider().parse(json); String name = JsonPath.read(document, "$.name"); // 获取name字段的 int age = JsonPath.read(document, "$.age"); // 获取age字段的 String firstCar = JsonPath.read(document, "$.cars[0]"); // 获取cars数组第一个元素的 System.out.println(name); System.out.println(age); System.out.println(firstCar); ``` 希望我的回答能帮助到你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值