Query a JSONObject in java

后端 未结 6 1678
鱼传尺愫
鱼传尺愫 2020-11-27 06:12

I was wondering if somewhere out there exists a java library able to query a JSONObject. In more depth I\'m looking for something like:



        
相关标签:
6条回答
  • 2020-11-27 06:50

    You can use org.json

        String json = "{ data: { data2 : { value : 'hello'}}}";
        org.json.JSONObject obj = new org.json.JSONObject(json);
        System.out.println(obj.query("/data/data2/value"));
    
    0 讨论(0)
  • 2020-11-27 06:59

    I think no way.

    Consider a java class

    class Student {
        Subject subject = new Subject();
    }
    
    class Subject {
        String name;
    }
    

    Here if we want to access subject name then

    Student stud = new Student();
    stud.subject.name; 
    

    We cant access name directly, if so then we will not get correct subject name. Like here:

    jsonObject.getAsJsonObject("data")
              .getAsJsonObject()
              .get("data2")
              .getAsJsonObject("value")
              .getAsString();
    

    If you want to use same like java object then use

    ClassName classObject = new Gson().fromJson(JsonString, ClassName.class);
    

    ClassName must have all fields to match jsonstring. If you have a jsonobject inside a jsonobject then you have to create separate class like I'm doing in Student and Subject class.

    0 讨论(0)
  • 2020-11-27 07:13

    While not exactly the same, Jackson has Tree Model representation similar to Gson:

    JsonNode root = objectMapper.readTree(jsonInput);
    return root.get("data").get("data2").get("value").asText();
    

    so you need to traverse it step by step.

    EDIT (August 2015)

    There actually is now (since Jackson 2.3) support for JSON Pointer expressions with Jackson. So you could alternatively use:

    return root.at("/data/data2/value").asText();
    
    0 讨论(0)
  • 2020-11-27 07:14

    Using Java JSON API 1.1.x (javax.json) one can make use of new JavaPointer interface. Instance implementing this interface can be considered to some extend as kind of XPath expression analog (see RFC-6901 for details). So in your case you could write this:

    import javax.json.*;
    //...
    var jp = Json.createPointer("/data/data2/value");
    System.out.println(jp.getValue(jsonObject));
    

    In 1.1.4 version of JSON there's also nice addition to JsonStructure interface (which is implemented by JsonObject and JsonArray), namely getValue(String jsonPointer). So it all comes down to this simple one-liner:

    System.out.println(jsonObject.getValue("/data/data2/value"));
    
    0 讨论(0)
  • 2020-11-27 07:17

    I've just unexpectedly found very interesting project: JSON Path

    JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document.

    With this library you can do what you are requesting even easier, then my previous suggestion:

    String hello = JsonPath.read(json, "$.data.data2.value");
    
    System.out.println(hello); //prints hello
    

    Hope this might be helpful either.

    0 讨论(0)
  • 2020-11-27 07:17

    First of all, I would recommend consider JSON object binding.

    But in case if you get arbitrary JSON objects and you would like process them in the way you described, I would suggest combine Jackson JSON processor along with Apache's Commons Beanutils.

    The idea is the following: Jackson by default process all JSON's as java.util.Map instances, meanwhile Commons Beanutils simplifies property access for objects, including arrays and Map supports.

    So you may use it something like this:

    //actually it is a Map instance with maps-fields within
    Object jsonObj = objectMapper.readValue(json, Object.class);
    
    Object hello = PropertyUtils.getProperty(jsonObj, "data.data2.value")
    
    System.out.println(hello); //prints hello
    
    0 讨论(0)
提交回复
热议问题