I have the following JSON object that is parsed through a HTTP request.
{
"firstName": "User",
"lastName": "Test",
"emailId": "testnew@gmail.com",
"formsAndQuestions": {
"Form1": {
"Question1": {
"value": "NEVER",
"isBoolean": false
},
"Question2": {
"value": "YES"
}
},
"Form2": {
"Question1": {
"value": "OTHER"
}
}
}
}
The form object can be dynamic. Therefore I'm deserializing this in my Java class with the following variable.
Map<String, Map<String, Map<String, String>>> forms;
Then I'm going through the complex loop below to iterate these and read the answer values.
for (Map.Entry<String, Map<String, Map<String, String>>> entry : input.formsAndQuestions().get().entrySet()) {
logger.log("Form: " + entry.getKey());
for (Map.Entry<String, Map<String, String>> entry1 : entry.getValue().entrySet()) {
logger.log("Question: " + entry1.getKey());
for (Map.Entry<String, String> entry2 : entry1.getValue().entrySet()) {
logger.log("key: " + entry2.getKey());
logger.log("value: " + entry2.getValue());
}
}
}
Is there a better way to do this? Loop seems pretty complex. I don't want to create a Java object class for deserializing the json as the "formsAndQuestions" object can have multiple forms added/removed with multiple questions/answers per form added or removed.
Would greatly appreciate any feedback on this approach.