AbuMariam
AbuMariam

Reputation: 3668

Json Java : retrieve attributes with particular value

I need to retrieve from a given JSON node only those attributes with particular value. In my case it is "Photorec" So for example...

                {

                    "Id": "a3TU0000008MMfwMAG",
                    "Measurement__c": "a3VU0000000huVaMAI",
                    "Available__c": false,
                    "Available_MT__c": "Photorec",
                    "Available_Ambient__c": false,
                    "Available_Ambient_MT__c": "Photorec",
                    "Available_Chilled__c": false,
                }

Here I would like to get back an array or list which has values "Available_MT__c" and "Available_Ambient_MT__c".

I am using Java's org.json.JSONObject in my code to represent this node.

Thanks in advance for any help.

Upvotes: 0

Views: 75

Answers (1)

Matej Špilár
Matej Špilár

Reputation: 2687

Try something like this :)

            ArrayList<String> foundValues = new ArrayList<String>();
            ArrayList<String> jsonStringValues= new ArrayList<String>();
            jsonStringValues.add("Id");
            jsonStringValues.add("Available_Ambient_MT__c");
            jsonStringValues.add("Available_MT__c");
            jsonStringValues.add("Measurement__c");

            JSONObject jObj = yourItems.getJSONObject(i);
            for(String node: jsonStringValues){
               if(jObj.getString(node).equals("Photorec")){
                  foundValues.add(node);
               } 
            }
            System.out.println("Values found: " + foundValues);

Upvotes: 1

Related Questions