I have a Json which contains array of Arrays, now I need to parse that Json and count the elements, and after reaching certain limit I need to put it into result Json. I was able to parse till one level and count the elements. How can I parse multiple levels and get the object in same format:
here is the sample code I tried for parsing one level with counting no.of elements:
private void handleJson(Object jsonObj, CountObject c, JSONObject jsonObj) {
Map<String, Object> map= new HashMap<>();
if (jsonObj instanceof JSONObject) {
parseJson(inputJSON,c, map, jsonObj);
}
}
}
private void parseJson(Object inputObj, CountObject c, Map<String, Object> map, JSONObject jsonObj) {
JSONObject nodeJson = (JSONObject) inputJSON;
Iterator<String> keyIter = nodeJson.keySet().iterator();
while (keyIter.hasNext()) {
String key = keyIter.next();
Object value = nodeJson.get(key);
if (value instanceof JSONObject) {
int offSet = c.getOffSet();
if(c.getLimit() == c.getOffSet()) {
break;
}
keyIter.remove();
map.put(key, value);
c.setOffSet(++offSet);;
} else {
handleJSONArray(value,k, map, key);
}
}
for (Entry<String, Object> entry : map.entrySet()) {
jsonObj.put(entry.getKey(), entry.getValue());
}
}
private void handleJSONArray(Object inputJSON, CountObject c, Map<String, Object> map, String key) {
JSONArray nodeJsonArr = (JSONArray) inputJSON;
int offSet = c.getOffSet();
List<Object> ll = new ArrayList<>();
for (int i = 0; i < nodeJsonArr.length(); i++) {
Object value = nodeJsonArr.get(i);
if (value instanceof JSONArray) {
handleJSONArray(value, c, map, key2);
} else {
if (k.getLimit() == k.getOffSet()) {
break;
}
ll.add(value);
++offSet;
}
}
map.put(key2, ll);
c.setOffSet(offSet);
}
and here is my Json :
{
"emails": [
{
"emails": [
{
"email": {
"id": "ac9e95cf-3338-4094-b465-e0e1deca23c4",
"value": "[email protected]"
}
}
]
},
{
"email": {
"id": "b61ffb48-ffc7-4ae6-81a2-78b632892fda",
"value": "[email protected]"
}
}
],
"lastName": {
"id": "ffe19ece-819b-4680-8e0b-8566b34c973d",
"value": "FirstName"
},
"firstName": {
"id": "4ed234f4-f679-40f3-b76b-41d9fdef7390",
"value": "LastName"
}
}
And count Object is a Pojo which has offset and Limit variables , If I pass limit as 3 I should fetch only first 3 elements with same json format something like below :
{
"emails": [
{
"emails": [
{
"email": {
"id": "ac9e95cf-3338-4094-b465-e0e1deca23c4",
"value": "[email protected]"
}
}
]
},
{
"email": {
"id": "b61ffb48-ffc7-4ae6-81a2-78b632892fda",
"value": "[email protected]"
}
}
],
"lastName": {
"id": "ffe19ece-819b-4680-8e0b-8566b34c973d",
"value": "FirstName"
}
Here I gave one of the sample JSON file, and Json can contain any no.of inner Array of elements, logic should be able to parse any type of Json. Here I should do the pagination as well for Json elements, means if I pass offSet and limit and I should fetch the elements accordingly. In the above example CountObject contains limit and offSet based on that it should fetch the elements. TO give more explanation If I pass offSet as 10 and limit a 10 I should fetch the elements in from 10th element to 20th element and so on.
JSONObjectwhich comes from somewhere else (probablyorg.json.JSONObject). Are you looking for a specific JSON library solution?