A
A
Alexander Nazarov2019-01-09 21:37:56
Java
Alexander Nazarov, 2019-01-09 21:37:56

How to get nested Json value?

I'm getting values ​​from json, but I don't know how to get nested value.
I need to get the value: "lastseen".

String vResult = "{\"ip\":{\"lastseen\":\"2019\"}}";
    System.out.println("vResult: " + vResult);
    try {
        org.json.simple.parser.JSONParser parser = new org.json.simple.parser.JSONParser();
        org.json.simple.JSONObject result = (org.json.simple.JSONObject) parser.parse(vResult);
        String value = (String) result.get("ip");

        System.out.println("value: " + value);

    } catch (org.json.simple.parser.ParseException e) {
        System.out.println("Error: " + e);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2019-01-09
@wergio

OMG.
1) learn how to import classes
2) take a normal parser, for example gson, moshi, jackson
3) if with your code, then you take value incorrectly. There is a getJSONObject method, and in the resulting object, do another getString.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question