json decode example

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
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
gudii9Author 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
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.