2

I have a JSONArray:

myArray => [{"key1" : "value1", "key2" : "value2"}, {"key3" : "value3", "key4" : "value4"}]

If I do myArray.get(1); I get a Object.

I want the single keys and values of myArray[1]. How can I do this? There is a way to have a HashMap that contains {"key3" : "value3", "key4" : "value4"} ?

Please help me, thanks.

Joaquin McCoy
  • 523
  • 1
  • 5
  • 11

2 Answers2

0

Instead of using myArray.get(), use myArray.getJSONObject() to return a JSONObject. You can call getString(key), getDouble(key), getInt(key), etc on the JSONObject to access the values. See the documentation at http://developer.android.com/reference/org/json/JSONObject.html and http://developer.android.com/reference/org/json/JSONArray.html

Chris Fei
  • 1,327
  • 8
  • 11
  • wrong, because I can't use myArray.getJSONObject(1), eclipse says I can't cast Object to a JSONObject. – Joaquin McCoy Dec 01 '10 at 00:49
  • I'm not sure I understand. Assuming `myArray` is a `JSONArray`, are you saying that `JSONObject myObject = myArray.getJSONObject(1);` is invalid? – Chris Fei Dec 01 '10 at 02:24
0

Use getJSONObject(1).getString("key3"); and put the returned value in your HashMap. I am not sure if there is an "automatic" way to do this. You can look at the documentation for more examples.

Zarah
  • 5,179
  • 2
  • 29
  • 29