The following example makes use of JSONObject and JSONArray where JSONObject is a java.util.Map and JSONArray is a java.util.List, so you can access them with standard operations of Map or List.import org.json.simple.JSONObject;import org.json.simple.JSONArray;import org.json.simple.parser.ParseException;import org.json.simple.parser.JSONParser;class JsonDecodeDemo { public static void main(String[] args){ JSONParser parser = new JSONParser(); String s = "[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]"; try{ Object obj = parser.parse(s); JSONArray array = (JSONArray)obj; System.out.println("The 2nd element of array"); System.out.println(array.get(1)); System.out.println(); JSONObject obj2 = (JSONObject)array.get(1); System.out.println("Field \"1\""); System.out.println(obj2.get("1")); s = "{}"; obj = parser.parse(s); System.out.println(obj); s = "[5,]"; obj = parser.parse(s); System.out.println(obj); s = "[5,,2]"; obj = parser.parse(s); System.out.println(obj); }catch(ParseException pe){ System.out.println("position: " + pe.getPosition()); System.out.println(pe); } }}On compiling and executing the above program, the following result will be generated −The 2nd element of array{"1":{"2":{"3":{"4":[5,{"6":7}]}}}}Field "1"{"2":{"3":{"4":[5,{"6":7}]}}}{}[5][5,2]
No, it is just converting a string containing data in JSONotation to indexable lists and maps (you can index into with a key) . [] builds a JSON array with no element, {a:1} a JSON object with a single element with key a. An object can consist of objects and arrays, too, so usually gets nested to some levels deep, and that is what you see with the example.
gudii9
ASKER
No, it is just converting a string containing data in JSONotation to indexable lists and map
is it converting json string to map?
which one is json string
below highlighted one?
public static void main(String[] args){
JSONParser parser = new JSONParser(); String s = "[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
below is map?
The 2nd element of array
{"1":{"2":{"3":{"4":[5,{"6":7}]}}}}
Field "1"
{"2":{"3":{"4":[5,{"6":7}]}}}
{}
[5]
[5,2]
what map is this java hashmap? or array list or something else?
please elaborate each step clearly