0

Was trying to use the android JSON object to parse a particular response, but am unable to make a code that would parse this response "{"r":{"f":[1,0,15,5948]}}".

Tried using below code, but am getting error :

"No value for f : {"r":{"f":[1,0,15,5948]}}"

Code is as follows:

String abc = "{\"r\":{\"f\":[1,0,15,5948]}}";

JSONObject json = new JSONObject(abc);

if (json.has("r")) {

Bundle b = new Bundle();

b.putInt("p", json.getJSONArray("f").getInt(0));

b.putInt("s", json.getJSONArray("f").getInt(1));

}

I intend to parse the above response and get the values in bundle respective variables. Like b.putInt("p", json.getJSONArray("f").getInt(0)); should get the 1 in f:[1....] and so forth.

Can somebody help in having a working code for getting the values for above reponse.

2 Answers 2

2

"f" is a sub element of "r", therefore you need to access it like this:

getJSONObject("r").getJSONArray("f")

Sign up to request clarification or add additional context in comments.

Comments

0

When you do:

b.putInt("p", json.getJSONArray("f").getInt(0));

the json variable still references root object of your json. You have to traverse down one level to access field f.

This works for me:

String abc = "{\"r\":{\"f\":[1,0,15,5948]}}";
JSONObject json = new JSONObject(abc);
if (json.has("r")) {
    json = json.getJSONObject("r");
    Bundle b = new Bundle();
    b.putInt("p", json.getJSONArray("f").getInt(0));
    b.putInt("s", json.getJSONArray("f").getInt(1));
}

Note line:

json = json.getJSONObject("r");

1 Comment

Thank you smuk, i would check if that help me get the values.

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.