0

I have an JSON something like:

{
"store":"usa",
"values":["1","2","3","4"];
}

and so I go ahead and get a JSON object for values:

JSONObject values = json.getJSONObject("values");

So now I have this Json object but methods like getName("X") does not work for this type of JSON. There is no key value for the Array now. Its just Strings oen after another.

I want it to return like

String[] listValues = value.getArray();

But I don't see anything like this.

Any ideas ?

Thanks !!

3
  • 1
    Thats not a valid json. Check it.. Commented Nov 14, 2013 at 17:12
  • Try using google-gson, its really easy! Look it up Commented Nov 14, 2013 at 17:14
  • The ; at the end of the array needs to go for this to be valid JSON. Is that present in the data or just a typo in the post? Commented Nov 14, 2013 at 17:22

2 Answers 2

3

Have you tried this?

JSONArray a = json.getJSONArray("values");
for (int i = 0; i < a.size(); i++) {
    Log.d("Type", a.getString(i););
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yep. The key is to use getJSONArray instead of getJSONObject. The answer would be improved if you edited it to show how to use a.getLength() to allocate the String[] and then assign elements (instead of logging).
Thanks that's what I was looking for ... I is feeling burnt out today !
0
String myJson = "{
"store":"usa",
"values":["1","2","3","4"]
}";

JSONObject jsonObject = new JSONObject(myJson);

JSONArray array = jsonObject.getJSONArray("values");

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.