简体   繁体   中英

Java JSON/GSON - search all objects within an array and return match

I am returning an JSON array which contains many objects, I'm looking to be able to search the attributes in each object and then return the objects that meet this criteria.

I am able to return the JSON array but I'm having trouble on how to then search through the objects to match the attribute values to a given value.

Some example values from the array:

[
  {"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"136199","timeStamp":1.483384640123117E9,"uID":"136199_3_10"},
  {"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"136199","timeStamp":1.483379834470379E9,"uID":"136199_3_10"},
  {"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"136199","timeStamp":1.483384639621985E9,"uID":"136199_3_10"}
]

I'm using the following code to return the array, which works as expected:

   JsonParser jp = new JsonParser(); 
   JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); 
   JsonArray rootArr = root.getAsJsonArray();

The following block of code is what I'm using to search through an object for the given attribute value, this code works when only an object is returned but gives an error when the whole array is returned:

   JsonObject rootObj = rootArr.getAsJsonObject();
   for (String attribute : attributes) {
     System.out.println(rootObj.get(attribute).getAsString());
   }

It is giving the error:

java.lang.IllegalStateException: Not a JSON Object:

I've tried changing rootObj.get(attribute) to rootArr.get(attribute) but that returns the error:

incompatible types: java.lang.String cannot be converted to int

This is the method call:

method("136199", Arrays.asList("blobJson", "deviceMfg", "uID"));

Method declaration:

 void method(String sensor, List<String> attributes)

The issue is that you're trying to treat JsonArray to JsonObject. Try the below code and see if it works for you. Point of interest for now is - JsonObject rootObj = rootArr.get(0).getAsJsonObject();

public static void main(String[] args) {

    String json = "[{\"blobJson\":\"x\",\"deviceMfg\":10,\"eventCode\":0,\"sensorClass\":3,\"sensorUUID\":\"136199\",\"timeStamp\":1.483384640123117E9,\"uID\":\"136199_3_10\"},{\"blobJson\":\"x\",\"deviceMfg\":10,\"eventCode\":0,\"sensorClass\":3,\"sensorUUID\":\"136199\",\"timeStamp\":1.483379834470379E9,\"uID\":\"136199_3_10\"},{\"blobJson\":\"x\",\"deviceMfg\":10,\"eventCode\":0,\"sensorClass\":3,\"sensorUUID\":\"136199\",\"timeStamp\":1.483384639621985E9,\"uID\":\"136199_3_10\"}]";

    JsonParser jp = new JsonParser();
    JsonElement root = jp.parse(json);
    JsonArray rootArr = root.getAsJsonArray();

    JsonObject rootObj = rootArr.get(0).getAsJsonObject();
    rootObj.entrySet().forEach(entry -> System.out.println(entry.getKey()+": "+entry.getValue().getAsString()));

}

Here is what you can try

 try {
    JSONArray jsonArray = new JSONArray(data);
    for (int i = 0; i < jsonArray.length(); i++) {
    Log.e("JSON Count", jsonArray.get(i).toString());
   }
} catch (Exception e) {
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM