0

I have a string called "response" like this:

{"test": {
   "id": 179512,
   "name": "Test",
   "IconId": 606,
   "revisionDate": 139844341200,
   "Level": 20
}}

I want to save id value to a variable. How can i do it?

JSONObject jsonObj = new JSONObject(response);  
jsonObj.getInt("id");

This dont work.

2 Answers 2

1

Try this..

JSONObject jsonObj = new JSONObject(response);  
JSONObject test_jsonObj = jsonObj.getJSONObject("test");  
int id = test_jsonObj.getInt("id");
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! And what about if there is an array inside of object?
Here is full documentation of JSONObject. There is a method called getJSONArray for such case.
@user3316779 You can get like getJSONArray into JSONArray.
1

You have to do:

JSONObject jsonObj = new JSONOBject(response);
JSONObject nestedObj = jsonObj.getJSONObject("test");
int id = nestedObj.getInt("id");

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.