json decode example

gudii9
gudii9 used Ask the Experts™
on
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]

Open in new window


i am not clear on above example. is it converting java object to json object?

what is meaning of decode
please advise
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
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.

Author

Commented:
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
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
First, let's format the JSON string (stored in s) properly:
[0,
 {"1":
   {"2":
     {"3":
       {"4":
         [5,
           {"6":7}
         ]
       }
     }
   }
 }
]

Open in new window

Note that there are strings and integers as key/values.

As said, [] builds an array, {} a map (hashtable).
So we have an array first with two elements, 0 and {"1": ...}. By indexing into it with 1 you get the second element (0 is the first element),
That element is a map with a single key "1" (string 1!) and another object as value. That object's string representation is then printed out by the code as Field "1".

The following lines are examples not related to the original JSON string, s gets overwritten with another JSON string for each output line.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial