当前位置: 动力学知识库 > 问答 > 编程问答 >

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.

网友答案:

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);
分享给朋友:
您可能感兴趣的文章:
随机阅读: