2

I have a json as below. I want to get mobile_number from this jsonObject.

json:-

{"id": "ABCD", "report": { "data": { "phone": { "mobile_number": 9876543210, "active": "Y", "content": null } } } }

I am doing it like this and it works fine but can someone help me with any other approach for it without getting every key.

JSONObject jsonObject = new JSONObject(json);
JSONObject report = getJSONObjectFromJson(jsonObject, "report");
JSONObject data = getJSONObjectFromJson(jsonObject, "data");
JSONObject phone = getJSONObjectFromJson(data, "phone");
long mobileNumber = getLongFromJson(phone, "mobile_number");

private Long getLongFromJson(JSONObject object, String key){
    return (object !=null && object.has(key)) ? object.getLong(key) : null;
}

private JSONObject getJSONObjectFromJson(JSONObject object, String key){
    return (object !=null && object.has(key)) ? object.getJSONObject(key) : null;
}
3
  • You could use a json mapper as gson for example. In this case you only define objects with the properties you want to access and apply the parse method Commented Nov 15, 2019 at 8:02
  • There isn't a way to do it without traversing the tree, one way or another. Commented Nov 15, 2019 at 8:02
  • 1
    I think the value of mobile_number should be wrapped by double quotes because it may starts with 0 which makes your JSON string be invalid. Commented Nov 15, 2019 at 8:28

4 Answers 4

2

I've just dealing with the similar issue and decided to use JsonPath like this:

final DocumentContext jsonContext = JsonPath.parse(jsonString);
final Object read = jsonContext.read("$['report']['data']['phone']['mobile_number']");
Sign up to request clarification or add additional context in comments.

Comments

2

You can use Jackson ObjectMapper.

        try {
            ObjectMapper mapper = new ObjectMapper();
            String jsonString = "{\"id\": \"ABCD\", \"report\": { \"data\": { \"phone\": { \"mobile_number\": 9876543210, \"active\": \"Y\", \"content\": null } } } }";
            JsonNode rootNode = mapper.readTree(jsonString);

            JsonNode mobileNumber = rootNode.path("report").path("data").path("phone").path("mobile_number");
            System.out.println("Mobile Number: " + mobileNumber.longValue());

        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

Comments

1

So there are lot of ways to do it but everything leads eventually to traversing the tree.

So to conclude all the approaches,

1. **Convert string to JsonObject and traverse.** 

    JSONObject jsonObject = new JSONObject(json);
    JSONObject report = getJSONObjectFromJson(jsonObject, "report");
    JSONObject data = getJSONObjectFromJson(jsonObject, "data");
    JSONObject phone = getJSONObjectFromJson(data, "phone");
    long mobileNumber = getLongFromJson(phone, "mobile_number");
    
    private Long getLongFromJson(JSONObject object, String key){
        return (object !=null && object.has(key)) ? object.getLong(key) : null;
    }
    
    private JSONObject getJSONObjectFromJson(JSONObject object, String key){
        return (object !=null && object.has(key)) ? object.getJSONObject(key) : null;
    }

 2. **Using jackson objectMapper to get the JsonNode and then traverse.**

    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode= mapper.readTree(json);
    JsonNode mobileNumber = jsonNode.path("report").path("data").path("phone").path("mobile_number");

 3. **Using gson jsonmapper to convert to map and then iterate the map.**

    Gson gson = new Gson();
    Map map = gson.fromJson(json, Map.class);

Comments

0
jsonObject.getJSONObject("x").getJSONObject("Y").getJSONObject("z");

Another route would be to leverage the ObjectMapper.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.