static String a = "{\n"
" \"name\": \"Praveen\",\n"
" \"address\": \"Avadi\",\n"
" \"Place\": \"Chennai\",\n"
" \"Friends\": [{\n"
" \"name\": \"kumaresh\",\n"
" \"address\": \"paruthipetu\",\n"
" \"Place\": [\"Chennai\", \"Mumbai\"],\n"
" \"Friends\": {\n"
" \"name1\": \"Midhun\",\n"
" \"name2\": \"vicky\"\n"
" }\n"
" },{\n"
" \"hair\": \"black\"\n"
" }],\n"
" \"number\": {\n"
" \"homeNo\": false,\n"
" \"phoneNo\": 8926363636\n"
" },\n"
" \"age\": 24,\n"
" \"age1\": 26\n"
"}";
public static ArrayList<Object> extractKeysValues(String test, String parentKey) throws Exception {
ArrayList<Object> arraylist = new ArrayList<Object>();
if(test.charAt(0)=='{') {
//object
JSONObject json_object = new JSONObject(test);
Iterator<String> keys = json_object.keys();
while(keys.hasNext()) {
String key = keys.next();
arraylist.add(key);
arraylist.add(json_object.get(key));
arraylist.add(parentKey);
if(json_object.get(key) instanceof JSONObject || json_object.get(key) instanceof JSONArray) {
arraylist.addAll(extractKeysValues(json_object.get(key).toString(),key));
}
}
} else if(test.charAt(0)=='[') {
//array
JSONArray json_array = new JSONArray(test);
int json_array_length = json_array.length();
for(int i=0;i<json_array_length;i ) {
String key = json_array.get(i).toString();
arraylist.add(String.valueOf(i));
arraylist.add(key);
arraylist.add(parentKey);
if(json_array.get(i) instanceof JSONObject || json_array.get(i) instanceof JSONArray) {
arraylist.addAll(extractKeysValues(json_array.get(i).toString(),String.valueOf(i)));
}
}
}
return arraylist;
}
System.out.println(extractKeysValues(a,"root"));
OUTPUT:
[number, {"homeNo":false,"phoneNo":8926363636}, root, homeNo, false, number, phoneNo, 8926363636, number, address, Avadi, root, Friends, [{"address":"paruthipetu","Friends":{"name2":"vicky","name1":"Midhun"},"name":"kumaresh","Place":["Chennai","Mumbai"]},{"hair":"black"}], root, 0, {"address":"paruthipetu","Friends":{"name2":"vicky","name1":"Midhun"},"name":"kumaresh","Place":["Chennai","Mumbai"]}, Friends, address, paruthipetu, 0, Friends, {"name2":"vicky","name1":"Midhun"}, 0, name2, vicky, Friends, name1, Midhun, Friends, name, kumaresh, 0, Place, ["Chennai","Mumbai"], 0, 0, Chennai, Place, 1, Mumbai, Place, 1, {"hair":"black"}, Friends, hair, black, 1, name, Praveen, root, Place, Chennai, root, age, 24, root, age1, 26, root]
EXPECTED OUTPUT:
[name, Praveen, root, address, Avadi, root, Place, Chennai, root, Friends,[{name:'kumaresh',address: 'paruthipetu',Place: ["Chennai","Mumbai"], Friends: {"name1":"Midhun","name2":"vicky"}},{ hair: 'black' }], root, 0,{name: 'kumaresh', address: 'paruthipetu', Place: [ 'Chennai', 'Mumbai' ], Friends: { name1: 'Midhun', name2: 'vicky' }}, Friends, name, kumaresh, 0, address, paruthipetu, 0, Place,[ 'Chennai', 'Mumbai' ], 0, 0, Chennai, Place, 1, Mumbai, Place, Friends, { name1: 'Midhun', name2: 'vicky' }, 0, name1, Midhun, Friends, name2, vicky, Friends, 1, { hair: 'black' }, Friends, hair, black, 1, number, { homeNo: false, phoneNo: 8926363636 }, root, homeNo, false, number, phoneNo, 8926363636, number, age, 24, root, age1, 26, root]
Can someone help me to achieve the expected output?
What I am trying to achieve in this code is, need to iterate over any array or object in order using java. The values stored in format key, value, parent key.
I have used recursion to solve this problem. Solution without recursion is also highly welcomed.
CodePudding user response:
With respect to iterating in 'the given order':
JSON objects are inherently unordered, so you're attempting something that cannot be done. Only JSON arrays are ordered.
An object in "{ ... }" is represented in Java as a map from keys to values, with no relationship between the original order, and iterating map keys.
The upshot is that a program that expects to maintain key-order across a round-trip through JSON is ill-founded.
With respect to recursion:
Since an object can contain arrays, and an array can contain objects, a recursive approach is the easiest and clearest way to write this.