Colin747
Colin747

Reputation: 5023

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)

Upvotes: 0

Views: 4191

Answers (2)

Ankit Khare
Ankit Khare

Reputation: 1385

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) {
}

Upvotes: 0

Pavan Kumar
Pavan Kumar

Reputation: 4820

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()));

}

Upvotes: 1

Related Questions