java 使用jsonpath,如何在Java中使用JsonPath从JSON获得价值?

I want to get the value from the JSON object using JsonPath.Could anyone please suggest me the appropriate jars which i would need because as per my knowledge i am getting this exception for the jars i am using for jsonpath .

package jsonPg;

import java.io.IOException;

import org.json.JSONException;

import org.json.JSONObject;

import com.jayway.jsonpath.JsonPath;

public class ReadJsonPath {

static String file = "D:\\AutomationSample\\Sample_Json.txt";

public static void main(String[] args) throws JSONException, IOException {

JsonReadFile jsonReadFile=new JsonReadFile();

JSONObject jsonObj=jsonReadFile.parseJSONFile(file);

String jsonObject=jsonObj.toString();

String json="";

System.out.println(jsonObject);

// Object val = JsonPath.read(jsonObject,"");

String val1=JsonPath.read(jsonObject," $.payload[*].supplierDataMap[*].COMPANYDETAILS.customFieldList[*].DISPLAYGSID .value");

System.out.println(val1);

}

}

here is the code which i have written and below is the exception which is thrown at runtime

Exception in thread "main" java.lang.NoSuchFieldError: FACTORY_SIMPLE

at com.jayway.jsonpath.spi.impl.JsonSmartJsonProvider.(JsonSmartJsonProvider.java:38)

at com.jayway.jsonpath.spi.impl.JsonSmartJsonProvider.(JsonSmartJsonProvider.java:41)

at com.jayway.jsonpath.spi.JsonProviderFactory. (JsonProviderFactory.java:24)

at com.jayway.jsonpath.Configuration.defaultConfiguration(Configuration.java:62)

at com.jayway.jsonpath.internal.JsonReader.(JsonReader.java:26)

at com.jayway.jsonpath.JsonPath.read(JsonPath.java:462)

at jsonPg.ReadJsonPath.main(ReadJsonPath.java:27)`

Any kind of help would be appreciated .

Thanks in advance .

解决方案

You can achieve your goal with JsonPath library on its own. Here is an example:

String jsonString = "{ \"list\": [ { \"name\": \"foo1\"}, { \"name\": \"foo2\"} ]}";

DocumentContext docCtx = JsonPath.parse(jsonString);

JsonPath jsonPath = JsonPath.compile("$.list[?(@.name == \"foo1\")]");

JSONArray val1=docCtx.read(jsonPath);

System.out.println(val1);

This code will print out:

[{"name":"foo1"}]

Required maven dependency:

com.jayway.jsonpath

json-path

2.2.0

json-path will also automatically pull json-smart JAR:

net.minidev

json-smart

2.2.1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值