0

My Rest API is returning the following response, in which only the inner list is required, all data shall be discarded:

{
  "meta": [],
  "links": [],
  "body": [
    {
      "meta": [],
      "links": [],
      "body": {
        "field1": "value1",
        "fieldn": "valuen"
      } // <-----
    },
    {
      "meta": [],
      "links": [],
      "body": {
        "field1": "value1",
        "fieldn": "valuen"
      } // <-----
    }
  ]
}

Is there any way in Gson or another other java library to fetch an array of the body or a straightforward way of doing that? Or maybe even using standard of java 8?

Or, should I use a standard iterator as follows:

//Old way to do this
JSONArray BodyArr = (JSONArray) jsonObject.get("Body");
Iterator<JSONObject> itBody = BodyArr.iterator();
int teller = 0;
while (itBody.hasNext()) {
  JSONObject bodyObj = itBody.next();
  JSONObject body = (JSONObject) bodyObj.get("Body");
}

Also in mysql we have way to do that using notation ($.body.body[] etc.). Is there any notational way to fetch the object

fatherazrael
  • 5,511
  • 16
  • 71
  • 155

2 Answers2

1

I think we have a nicely written article on this.

Json object iteration

  • 1
    Thanks Mubaraka. I can see it is again For each based iteration. I can use it but i am looking for some kind a notational approach if available to avoid looping or some otehr straight forward way. If not found this would be my answer. Thank you very much. – fatherazrael Nov 26 '19 at 11:05
1

If you have a class that represents an object in the array, then you can deserialize the JSONArray to an array of that class using public <T> T fromJson​(JsonElement json, java.lang.Class<T> classOfT) throws JsonSyntaxException on the Gson class:

class BodyItem {
    public String[] meta;
    public String[] links;
    public String field1;
    public String fieldn;
}

public BodyItem[] getBodyItems(final Gson gson, final JsonObject jsonObject) {
    final JsonElement body = jsonObject.get("body");
    return gson.fromJson(body, BodyItem[].class);
}

public static void main(final String[] args) {
    final String response = "<your REST API JSON response>";
    final Gson gson = new Gson();
    final JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
    final BodyItem[] bodyItems = getBodyItems(gson, jsonObject);
}

If you want a more notational way of accessing fields in Gson objects, you can use JsonObject's convenience accessors:

  • JsonArray getAsJsonArray​(java.lang.String memberName)
  • JsonObject getAsJsonObject​(java.lang.String memberName)
  • JsonPrimitive getAsJsonPrimitive(java.lang.String memberName)

And then with a JsonArray, you can iterate with for (final JsonElement element : jsonArray) or .forEach, and you can get JsonElements with the JsonElement get(int i) accessor.

So, say you had your original JsonObject response and wanted to get the value of body.field1 in the second element of the body list, you might do:

String value = jsonObject
    .getAsJsonArray("body")
    .get(1)
    .getAsJsonObject()
    .getAsJsonObject("body")
    .getAsJsonObject("field1");
Billy Brown
  • 2,272
  • 23
  • 25