4

I have a Json array, i want to get only one Json object from it. In this example how can i get the object with Apple

[
{
"name": "mango",
"use": "DA",
"date": "2011-09-26",
"seed": "31341"
},

{
"name": "apple",
"use": "DA",
"date": "2011-09-26",
"seed": "31341"
},

{
"name": "berry",
"use": "DA",
"date": "2011-09-26",
"seed": "31341"
}
]

Previously i used to get it via it's index position but since json doesn't guarantee me order/arrangement that's why i need to specifically get one object without using the index method.

Ceddy Muhoza
  • 636
  • 3
  • 17
  • 34

3 Answers3

7

You can use a loop to iterate over every item in the JSONArray and find which JSONObject has the key you want.

private int getPosition(JSONArray jsonArray) throws JSONException {
        for(int index = 0; index < jsonArray.length(); index++) {
            JSONObject jsonObject = jsonArray.getJSONObject(index);
            if(jsonObject.getString("name").equals("apple")) {
                return index; //this is the index of the JSONObject you want
            } 
        }
        return -1; //it wasn't found at all
    }

You could also return the JSONObject instead of the index. Just change the return type in the method signature as well:

private JSONObject getPosition(JSONArray jsonArray) throws JSONException {
        for(int index = 0; index < jsonArray.length(); index++) {
            JSONObject jsonObject = jsonArray.getJSONObject(index);
            if(jsonObject.getString("name").equals("apple")) {
                return jsonObject; //this is the index of the JSONObject you want
            } 
        }
        return null; //it wasn't found at all
    }
Jason John
  • 934
  • 2
  • 12
  • 23
1

You can use the jackson library with ObjectMapper

// Create a pojo for the json object
public class MyObject {
    public String name;
    public String use;
    public String date;
    public String seed;
}

...
public MyObject getApple(String jsonString) throws IOException {
    // the string type is MyObject array
    MyObject[] myObjects = new ObjectMapper().readValue(jsonString, MyObject[].class);
    for (MyObject myObject : myObjects ){
        if myObject.name.equals("apple") {
            return myObject;
        }
    }
    return null;
}
flakes
  • 21,558
  • 8
  • 41
  • 88
0

Because you can't rely on indecies you'll have to use another identifier. The best one would propably be the "name" field.

To find the one object with a specific name you'll have to iterate over the array and check every object, this is my code for doing that, it may not be the best or most efficient way but it works. The example uses GSON, but it should be easily adaptable:

/**
 * Function for getting an object with a specific name from an array.
 * 
 * @param arr The JsonArray to check in.
 * @param name The name to check for.
 * @return The JsonObject with a matching name field or null if none where found. 
 */
public static JsonObject getObjectWithName(JsonArray arr, String name)
{
    //Iterate over all elements in that array
    for(JsonElement elm : arr)
    {
        if(elm.isJsonObject()) //If the current element is an object.
        {
            JsonObject obj = elm.getAsJsonObject();

            if(obj.has("name")) //If the object has a field named "name"
            {
                JsonElement objElm = obj.get("name"); //The value of that field

                //Check if the value is a string and if it equals the given name
                if(objElm.isJsonPrimitive() && objElm.getAsJsonPrimitive().isString() && objElm.getAsString().equals(name))
                {
                    return obj;
                }
            }
        }
    }

    //Nothing matching was found so return null
    return null;
}
tim4242
  • 141
  • 2
  • 7