1

So I have this JSON structure I'm getting as a HTTP response. Structure looks like this:

{
    "field1": "value1",
    "field2": {
        "field2-2": [
            {
                "field2-2-1": "some value",
                "field2-2-2": {
                    "key" : "some value"
                }
            }
        ]
    }
}

Of course I simplified it but you get the idea. Now I use Gson to convert it into a HashMap:

HashMap<String, Object> resultMap = new Gson().fromJson(httpResult, type);

Now, to get to the "field2-2-2" in Java I do this:

LinkedTreeMap someMap = (LinkedTreeMap) resultMap.get("field2");
ArrayList<LinkedTreeMap> someList = (ArrayList) someMap.get("field2-2");
LinkedTreeMap anotherMap = someList.get(0);
anotherMap.get("key");

Once again I simplified it, but is there any better way to access this "deep" field? Or for the sake of readability can I chain methods somehow? What I mean is if this all have been ArrayLists I could do something like:

ArrayList<Arraylist<String>> sampleList = new ArrayList<>();
sampleList.get(0).get(0);
  • 4
    Did you try deserialising this structure directly into a POJO that matches? – D. Lawrence Nov 27 '19 at 08:39
  • 2
    Can you use libraries like `Jackson` or `JsonPath`? If yes, they allow you to provide path like `/field2/field2-2/field2-2-2/key`. `Jackson` supports [JSON Pointer](https://tools.ietf.org/html/rfc6901). Take a look also at: [Parsing deeply nested JSON properties with Jackson](https://stackoverflow.com/questions/57978790/parsing-deeply-nested-json-properties-with-jackson), [Iterate over a large JSON Array with JSONPath](https://stackoverflow.com/questions/55366515/iterate-over-a-large-json-array-with-jsonpath) – Michał Ziober Nov 27 '19 at 09:12
  • 1
    Michał Ziober, that is the most usefull response here I think, thank you – Max Urbanowicz Nov 27 '19 at 09:14
  • @MaxUrbanowicz, it should be easy to use `JsonPath` or `JsonPointer` with `Jackson`. In case you have found problems with those just update a question with code you have and ask new questions. – Michał Ziober Nov 27 '19 at 10:13
  • I'd propose to think about stream oriented parsing rather than object mapping. Both Gson and Jackson support it. Also, you can take a look at an example of simple and fast parser https://github.com/anatolygudkov/green-jelly – AnatolyG Dec 04 '19 at 13:58

1 Answers1

2

You could directly map it to POJO classes. Like:

@JsonInclude(Include.NON_NULL)
public class ApiResponse {

    String field01;
    Field2Class field02;

}

Field2Class.class

public class Field2Class {

   ArrayList<Field02_2> field02_2;

}

Field02_2.class

public class Field02_2 {

   String field02_2_1, field02_2_2;

}

With each class having getters, setters and default constructors.

Rahul Agrawal
  • 623
  • 1
  • 5
  • 18
  • I tried that approach using some online JSON -> POJO converter but the problem is my JSON is very huge and my POJO would be like 50 objects that is why I chose HashMap which Gson converts into just fine. I just wonder if I could access deep fields any better – Max Urbanowicz Nov 27 '19 at 08:51
  • If you use POJOs then you have to only once declare all the fields in the classes, then when you receive HTTP response, it will be directly mapped to these POJOs. So if in future, you have some extra fields updated/deleted, you only have to change this JAVA file. Also to get any field you could simply call getFieldMethod() rather then mapping it to Map first and then fetching its nested fields using nested mapping as you have done. Again if your response changes a lot of code changes would be there wherever you have to fetch that field. – Rahul Agrawal Nov 27 '19 at 08:56
  • Regarding huge JSON, it would be helpful if you could provide the complete JSON to have better understanding – Rahul Agrawal Nov 27 '19 at 09:07