Home > Mobile >  Java get nested value from ResponseEntity without creating a pojo
Java get nested value from ResponseEntity without creating a pojo

Time:12-30

I am trying to get a single nested value from a ResponseEntity but I am trying to do so without having to create a pojo for every possible item as this is a third party api response.

Example response.getBody() as JSON:

{
    "message": "2 records found",
    "records": [
        {
            "Account": {
                "Id": "1",
                "Name": "Foo Inc"
            },
            "CaseNumber": "200",
            "Contact": {
            "FirstName": "Foo",
            "LastName": "Bar"
            },
            "Status": "In Progress",
            "StatusMessage": "We are working on this."
        },
        {
            "Account": {
                 "Id": "1",
                 "Name": "Foo Inc"
            },
            "CaseNumber": "100",
            "Contact": {
                "FirstName": "Foo",
                "LastName": "Bar"
            },
            "Status": "Closed"
        }

    ]
}

Basically, if I were in JS, I am looking for:

for(let record of res.body.records){
   if(record && record.CaseNumber === "200"){
      console.log(record.Status)
}
res.body.records[0].Status

Currently, they are are doing this to check if the response is empty:

LinkedHashMap<Object, Object> resMap = (LinkedHashMap<Object, Object>) response.getBody();
        
List<Object> recordsList = (List<Object>) resMap.get("records");

if (recordsList.size() <= 0) { return error }

But I need to get the value of of "Status" and I need to do so without creating a pojo.

I appreciate any guidance on how I can do this in Java

CodePudding user response:

PLEASE DO NOT use Genson as Hiran showed in his example. The library hasn't been updated since 2019 and has many vulnerable dependencies!

Use Jackson or Gson.

Here how you can serialize a string into a Jackson JsonNode:

ObjectMapper mapper = new ObjectMapper();

String json = ...;
JsonNode node = mapper.readTree(json);

If you want to serialize a JSON object string into a Map:

ObjectMapper mapper = new ObjectMapper();

String json = ...;
Map<String, Object> map = mapper.readValue(json, HashMap.class);

You can read more about JsonNode here and a tutorial here.

CodePudding user response:

You can use JSON-Java library and your code will look like this:

JSONObject jsonObject = new JSONObject(JSON_STRING);
String status = jsonObject.getJSONArray("records")
                .getJSONObject(0)
                .getString("Status");
System.out.println(status);

Or in a loop

JSONArray jsonArray = new JSONObject(jsonString).getJSONArray("records");
for(int i =0; i < jsonArray.length(); i  ) {
    String status = jsonArray
                .getJSONObject(i)
                .getString("Status");
    System.out.println(status);
}

CodePudding user response:

Use a JSON parser to parse the body. If you do not want to create a POJO, simply use generic classes. Here is an example of using Genson with Java Collections. Once you are there, just navigate the collections and do the needful:

http://genson.io/GettingStarted/#java-collections

So for your case this should work:

Map<String, Object> body = genson.deserialize(response.getBody(), Map.class);
List records = (List)body.get("records");
for (Object o: records) {
    ...
}
  • Related