0

Using GSON, how can i return a single key from a Multidimensional Json String?

Here is the Multidimensional Json String:

{"statusCode":0,"statusDescription":"OK","data":{"user":{"id":xxx,"company_id":xxx,"account_type":"5","enable_locations":true,"intuit_user_id":null,"nick_name":"xxx","is_owner":"1","enabled":"1"},"session_token":"xxx"}}

I want to return the "session_token" key value.

I'm trying this:

class app {
    static class Response {
        String session_token;
    }
    public void getSessionToken() {

        String x = {"statusCode":0,"statusDescription":"OK","data":{"user":{"id":xxx,"company_id":xxx,"account_type":"5","enable_locations":true,"intuit_user_id":null,"nick_name":"xxx","is_owner":"1","enabled":"1"},"session_token":"xxx"}}
        Response r = new Gson().fromJson(x, Response.class);
        System.out.println(r.session_token);
    }
}

But with this, my r.session_token returns null.

3
  • huh? So you are manually creating a JSON string on the server side and trying to get the value by supplying the key? Commented May 20, 2013 at 19:36
  • @wirey No.... i just made the example. I'm retrieving the JSON String from a webserver and i need to parse it. But i need ONLY a single key, nothing else. Commented May 20, 2013 at 19:40
  • @wirey, what? getParameter are not only for GET and POST parameters? How getParameter will help me to get a string that is on page (retrieved from webserver)? The string is like this: http://setster.com/api/v2/company/6877/service Commented May 20, 2013 at 19:44

1 Answer 1

5

You would need to use Gson's JsonParser class directly and extract the data from the parse tree:

String myJsonString = "{\"name\":\"john\",\"lastname\":\"smith\"}";
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(myJsonString); 
JsonObject jsonObject = element.getAsJsonObject();
String lastName = jsonObject.get("lastname").getAsString();
System.out.println(lastName);

That said, it's debatable whether this would save you any real time over:

(edited from comments below):

class App {

    static class Response {
        String lastname;
    }

    public static void main(String[] args) {
        String myJsonString = "{\"name\":\"john\",\"lastname\":\"smith\"}";
        Response r = new Gson().fromJson(myJsonString, Response.class);
        System.out.println(r.lastname);
    }
}

Gson will silently ignore the fact that there's more data in the JSON than you're interested in, and later on you might be interested in it, in which case it's trivial to add fields to your Response class.

Edit due to question changing:

You have a JSON object. It contains a field data whose value is an object. Inside that object you have a field session_token that you're interested in.

Either you have to navigate to that field through the parse tree, or you have to create Java classes that all will map to. The Java classes would resemble (at the bare minimum):

class Response {
    Data data;
}

class Data {
    String session_token;
}
Sign up to request clarification or add additional context in comments.

4 Comments

How retrieve the lastname key? r.lastname? I've tried but it returns null.
See edit. Note your field names in your Java class and the field names in your JSON must match including case unless you annotate your Java class fields with @SerializedName to map them to something else. In a real application you'd have a getter in the Response class that returned the value.
I think it is because my JSON string are multidimensional. I will edit my question with this.
Well, whatever your real JSON is, you either have to traverse the parse tree to get to what you want, or create a Java class that maps to your JSON correctly.

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.