Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android JSONObject : Search function

I am trying to search from JSON code that has been received beforehand..

I was wondering are there any efficient way to search specific object? Or should i use straight forward search like looping and if statement?

I have parsing the JSON successfully, and it stored in my String variable.. The JSON contains Array of Objects..

Now i want to search for specific ObjectName and get the Whole Objects.. The JSON will be like

   [
     {
         "name":"blank",
         "date":"2014-06-05T00:44:30Z",
         "boolean":null,
     },
     {
         "name":"hello",
         "date":"2013-05-04T00:43:20Z",
         "boolean":null,
     }
   ]

I want to search for name = "hello" and get the last Array returned

Can anyone show me how?

Thanks a lot!

like image 414
AlbertSamuel Avatar asked Jun 11 '26 01:06

AlbertSamuel


1 Answers

You can use something like this:

String name;
JSONObject searchObject;

JSONArray array = new JSONArray(yourJsonString);

for (int i = 0; i < array.length(); i++) {
    JSONObject currObject = array.getJSONObject(i);
    name = currObject.getString("name");

    if(name == "hello")
    {
       searchObject = currObject        
    }
}

return searchObject
like image 58
Shai Aharoni Avatar answered Jun 13 '26 02:06

Shai Aharoni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!