Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get value by key jsonarray

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

like image 358
mmu36478 Avatar asked Mar 02 '17 07:03

mmu36478


People also ask

How do I get items from JSONArray?

JSONArray jsonArray = (JSONArray) jsonObject. get("contact"); The iterator() method of the JSONArray class returns an Iterator object using which you can iterate the contents of the current array.

What is key-value in JSON?

A JSON object contains zero, one, or more key-value pairs, also called properties. The object is surrounded by curly braces {} . Every key-value pair is separated by a comma. The order of the key-value pair is irrelevant. A key-value pair consists of a key and a value, separated by a colon ( : ).

What is the difference between JSON object and JSONArray?

JSONObject and JSONArray are the two common classes usually available in most of the JSON processing libraries. A JSONObject stores unordered key-value pairs, much like a Java Map implementation. A JSONArray, on the other hand, is an ordered sequence of values much like a List or a Vector in Java.

Can we convert JSONArray to JSON object?

Core Java bootcamp program with Hands on practiceWe can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method.


3 Answers

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();
        }
    }
like image 50
Chetan Joshi Avatar answered Nov 10 '22 02:11

Chetan Joshi


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

like image 43
Ravi MCA Avatar answered Nov 10 '22 03:11

Ravi MCA


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.

like image 5
dwhite5914 Avatar answered Nov 10 '22 01:11

dwhite5914