本文介绍了如何在Java中使用JsonPath从JSON获得价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JsonPath从JSON对象获取值,有人可以建议我使用我需要的合适的jars,因为据我所知,我正在为jsonpath使用的jars获得此异常.

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.<init>(JsonSmartJsonProvider.java:38)
at com.jayway.jsonpath.spi.impl.JsonSmartJsonProvider.<init>(JsonSmartJsonProvider.java:41)
at com.jayway.jsonpath.spi.JsonProviderFactory.<clinit>  (JsonProviderFactory.java:24)
at    com.jayway.jsonpath.Configuration.defaultConfiguration(Configuration.java:62)
at com.jayway.jsonpath.internal.JsonReader.<init>(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 .

推荐答案

您可以单独使用JsonPath库实现您的目标.这是一个示例:

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);

此代码将打印出来:

[{"name":"foo1"}]

所需的maven依赖项:

Required maven dependency:

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.2.0</version>
</dependency>

json-path还将自动拉出json-smart JAR:

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

<dependency>
    <groupId>net.minidev</groupId>
    <artifactId>json-smart</artifactId>
    <version>2.2.1</version>
</dependency>

这篇关于如何在Java中使用JsonPath从JSON获得价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 05:29