4

This is my first time working with json and I'm having a hard time trying to filter an array I'm trying to get all values (tienda) where id_estado=1.

Any clue how to approach this?

JA1= [{"id_estado":"1","tienda":"Aurrera"},{"id_estado":"1","tienda":"Walt-Mart"},{"id_estado":"1","tienda":"Chedraui"},{"id_estado":"1","tienda":"Soriana"},{"id_estado":"1","tienda":"Comercial MX"},{"id_estado":"1","tienda":"City Market"},{"id_estado":"2","tienda":"Walt-Mart"},{"id_estado":"2","tienda":"Chedraui"},{"id_estado":"2","tienda":"Aurrera"},{"id_estado":"2","tienda":"Superama"}]

            JSONArray JA1=new JSONArray(result1);
        JSONObject json1= null;
        name = new String[JA1.length()];
            for (int i = 0; i < JA1.length(); i++) {
                 Log.d("LOG", "resultado name sin arreglo11 " + JA1);
                //name[i] = json1.getString("tienda");
                //name[i]=json1.getString("estado");
            }

        for(int i=0;i<name.length;i++)
        {
            list2.add(name[i]);
            Log.d("LOG", "resultado name2 " + name[i]);
        }

Thanks in advance

1
  • Hint: If you are filtering the array, the resultant array will likely be smaller. The size of the resultant array will not be known up-front, so you should use an ArrayList<String>, not a String[], to collection the result. Commented Sep 10, 2017 at 4:19

3 Answers 3

9

Here's how I'd do it:

    try {
        List<String> filtered = new ArrayList<>();
        JSONArray ary = new JSONArray(/* your json here */);

        for (int i = 0; i < ary.length(); ++i) {
            JSONObject obj = ary.getJSONObject(i);
            String id = obj.getString("id_estado");
            if (id.equals("1")) {
                filtered.add(obj.getString("tienda"));
            }
        }
    } catch (JSONException e) {
        // handle exception
    }

An explanation of this code:

  • Create an ArrayList to hold the filtered values.
  • Create a JSONArray from your json text.
  • Iterate over each element in the array, pulling out a JSONObject each time.
  • For each object, we get its id_estado property and check if it is equal to "1".
  • If it is, we add the tienda property to the list of filtered values.
Sign up to request clarification or add additional context in comments.

Comments

0

If you're using kotlin, you can create an extension method and pass in a validation function as a parameter

fun JSONArray.filter(validate: (JSONObject) -> Boolean): List<JSONObject> {
    val retVal = mutableListOf<JSONObject>()
    for (i in 0.until(length())) {
        try {
            val jsonObject = getJSONObject(i)
            if (validate(jsonObject)) {
                retVal.add(jsonObject)
            }
        } catch (e: Exception) {
            Log.d(TAG, "Exception: $e")
        }
    }
    return retVal
}

You can use it like this

val filteredArray = jsonArray.filter { it.getString("bar") == "foo" }

Comments

0

try this

for (int i = 0; i < JA1.length(); i++) {
             JSONObject json_data = jsonArray.getJSONObject(i);
             name[i] = json_data.getString("tienda");

        }

we have to create object of each nested JSON item in the JSON using JSONObject json_data = jsonArray.getJSONObject(i); that was missing in your case.

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.