theJava
theJava

Reputation: 15034

Accessing JsonString using Gson

I am getting a response like this

String result = responseEntity.getBody();

{
    "FailureReason": "",
    "State": "True",
    "UserId": 1038,
    "Specified": false,
    "Name": "Java"
}

How would i access these JsonString. I am using Gson to form the JsonString. I am JS Guy, when i try to access result.Name [It throws me error]

Upvotes: 0

Views: 70

Answers (2)

hemu
hemu

Reputation: 3261

A good way is to use POJO. Make one POJO that represent the response. Use,

gson.fromJson(responseStr, responsePojoType);

This will return object of your POJO type. Then use POJO object's getter method to fetch whatever value required.

Upvotes: 1

Nermeen
Nermeen

Reputation: 15973

You can use Android's JSONObject like this:

JSONObject jsonResult = new JSONObject(result);
String name = jsonResult.getString("Name");
int id = jsonResult.getInt("UserId");

check http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

Upvotes: 1

Related Questions