2

I am connecting to a third party API and getting back a long JSON string. I only need one value from it, but it is located pretty deep inside the hierarchy. Is there a simple way to get it, without going through the whole thing? I looked all over but nothing seems easy.

Here's my example:

"response":{"status":1,"httpStatus":200,"data":{"myDesiredInfo":"someInfo"},"errors":[],"errorMessage":null}}

I've been trying to use Gson so I can get this blob as a JsonObject. I was sure there's something simple, like this:

jsonObject.get("myDesiredInfo") 

or at the minimum something like this:

jsonObject.get("response.data.myDesiredInfo") 

But it doesn't seem to exist.

So is there any parser out there that will allow me to do this?

Parvathy
  • 2,275
  • 3
  • 24
  • 39
Eddy
  • 3,533
  • 13
  • 59
  • 89
  • 1
    Which language are you using to interpret the json? JavaScript, c# etc... – Dan Feb 07 '13 at 10:02
  • Ahhh Java, See this question's answer... http://stackoverflow.com/questions/1395551/convert-a-json-string-to-object-in-java – Dan Feb 07 '13 at 10:09
  • Thanks, Dan, the first answer allows me to get the top level value, but nothing there really drills down to a lower level value. Look like in my situation I may as well simply parse this as any other java String. – Eddy Feb 07 '13 at 10:28
  • I'm not sure you understand. If you create a class with he same properties as the json you can cast the json into an object of type 'yourClass' . This would enable you to do myclassobject.Response.Data.Mydesiredinfo – Dan Feb 07 '13 at 11:15
  • You can autogen the class from the json using is link http://jsongen.byingtondesign.com/ – Dan Feb 07 '13 at 11:18
  • Thanks, Dan. For my needs it's an overkill, but it's good to know this is possible for future projects. – Eddy Feb 07 '13 at 13:52

4 Answers4

4

This is my json string

String s="{"age":0,"name":"name","email":"emailk","address":{"housename":"villa"}}";

I use following code to get housename

    JsonElement je = new JsonParser().parse(s);
    JsonObject asJsonObject = je.getAsJsonObject();
    JsonElement get = asJsonObject.get("address");
    System.out.println(s + "\n" + get);
    JsonObject asJsonObject1 = get.getAsJsonObject();
    JsonElement get1 = asJsonObject1.get("housename");
    System.out.println(get1);

The Following is my output :

{"age":0,"name":"name","email":"emailk","address":{"housename":"villa"}}
{"housename":"villa"}
"villa"

I don't think there is another way to do this. I also tried to do in other ways but i didn't get any output.

Parvathy
  • 2,275
  • 3
  • 24
  • 39
  • Yeah, this does it. Still, a bit disappointing. But if nobody else has a more elegant way I'll except this answer. Thanks, Parvathy! – Eddy Feb 07 '13 at 10:54
4

The following way you can retrieve from your jsonObject.

JSONObject jObject = new JSONObject(yourresponse);
Log.i("Desired Info is ",jObject.getJSONObject("response").getJSONObject("data").getString("myDesiredInfo"));
Harish
  • 3,122
  • 2
  • 31
  • 46
1

I wrote a little utility method that uses Gson's API to get a value as String from a JSON object, based on a java.util.List of values. So for my original question the list objects will be "response", "data", "myDesiredInfo."

Surely this can be improved on, but it's a start.

/*
 * Takes a JsonObject and parses it for a primitive value, going level by level
 * according to the values in @infos
 */
public static String parseJson(JsonObject json, List<String> infos) {

    try {
        if(infos.size() == 0) {
            return json.toString();
        }

        JsonElement je = json.get((String)infos.get(0));
        infos.remove(0);

        if(je instanceof JsonObject) {
            return parseJson(je.getAsJsonObject(), infos);          
        } else {
            return je.getAsString();
        }           
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Eddy
  • 3,533
  • 13
  • 59
  • 89
  • I was set out to write a similar snippet for my project, but wanted to check if there is an elegant way of doing this in Java. Looks like there isn't (Why am I even writing this comment!!??). – Vikas Dec 08 '17 at 03:34
0

Most languages have a JSON decoding library, a lot of them native. No idea what language you're using so here's PHP as an example:

$jsonObj = json_decode($json);
$json->response->data->myDesiredInfo;

Ruby, Python, Java - all these languages have good libraries.

Dan Blows
  • 20,846
  • 10
  • 65
  • 96