12
JSONArray arr = 
[
    {"key1":"value1"},
    {"key2":"value2"},
    {"key3":"value3"},
    {"key4":"value4"}
]

arr.get("key1") throws error. How can I get the value by key in JSONArray?

arr.getString("key1") also throws error. Should I loop through the array? Is it the only way to do it?

What is the error?

In Eclipse Debug perspective, these expressions returns as; error(s)_during_the_evaluation

SantiBailors
  • 1,596
  • 3
  • 21
  • 44
mmu36478
  • 1,295
  • 4
  • 19
  • 40
  • http://stackoverflow.com/questions/1568762/accessing-members-of-items-in-a-jsonarray-with-java – Jack Flamp Mar 02 '17 at 07:52
  • That is not valid Java. But anyway, it's an JSON *array*. So you need to get the element at the *index* you want in the array. That is an object. And the object has keys and values. – JB Nizet Mar 02 '17 at 07:57
  • 2
    `arr.getJSONObject(0).getString("key1")` returned what I want. Actually rather than loop through the array, I wanted to get the value as `arr.key1` as it is in js. But I guess I should loop through the array to find the index, am I right? – mmu36478 Mar 02 '17 at 08:00
  • Yes, you need to loop. And you need to decide what to do if several objects have that key. And, it's not like that in the JS. You would have to do the exact same thing in JavaScript with such a structure. – JB Nizet Mar 02 '17 at 08:02

4 Answers4

27

You can parse your jsonResponse like below code :

private void parseJsonData(String jsonResponse){
        try
        {
            JSONArray jsonArray = new JSONArray(jsonResponse);

            for(int i=0;i<jsonArray.length();i++)
            {
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                String value1 = jsonObject1.optString("key1");
                String value2 = jsonObject1.optString("key2");
                String value3 = jsonObject1.optString("key3");
                String value4 = jsonObject1.optString("key4");
            }
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
    }
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
5

Sounds like you want to find a specific key from an array of JSONObjects. Problem is, it's an array, so you have to iterate over each element. One solution, assuming no repeat keys is...

private Object getKey(JSONArray array, String key)
{
    Object value = null;
    for (int i = 0; i < array.length(); i++)
    {
        JSONObject item = array.getJSONObject(i);
        if (item.keySet().contains(key))
        {
            value = item.get(key);
            break;
        }
    }

    return value;
}

Now, let's say you want to find the value of "key1" in the array. You can get the value using the line: String value = (String) getKey(array, "key1"). We cast to a string because we know "key1" refers to a string object.

dwhite5914
  • 96
  • 5
4
for (int i = 0; i < arr.length(); ++i) {

    JSONObject jsn = arr.getJSONObject(i);

   String keyVal = jsn.getString("key1");
}

You need to iterate through the array to get each JSONObject. Once you have the object of json you can get values by using keys

Ravi MCA
  • 2,491
  • 4
  • 20
  • 30
-5

You can easy get a JSON array element by key like this:

var value = ArrName['key_1']; //<- ArrName is the name of your array
console.log(value);

Alternatively you can do this too:

var value = ArrName.key_1;

That's it!

Jhollman
  • 2,093
  • 24
  • 19