jsonobject encode and decode

hi,
i am working on below example
https://www.tutorialspoint.com/json/json_java_example.htm

what is meaning of json encode and decode means?

does it means convert to json from java object and vice versa?

 String s = "[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
what is above one , is it java String? which we are converting to json string by decoding?

package com.gp.json;
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}]}}}}]";
      //String s = { "name":"John" };
		
      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);
      }
   }
}

Open in new window


i got below output
The 2nd element of array
{"1":{"2":{"3":{"4":[5,{"6":7}]}}}}

Field "1"
{"2":{"3":{"4":[5,{"6":7}]}}}
{}
[5]
[5,2]

below code gave json as output
package com.gp.json;
import org.json.simple.JSONObject;

class JsonEncodeDemo {

   public static void main(String[] args){
      JSONObject obj = new JSONObject();

      obj.put("name", "gp");
      obj.put("num", new Integer(100));
      obj.put("balance", new Double(1000.21));
      obj.put("is_eligible", new Boolean(true));

      System.out.print(obj);
   }
}

Open in new window

{"balance":1000.21,"num":100,"name":"gp","is_eligible":true}
does it mean we are encoding to a json object from java object

i am confused with encoding and decoding
what is JSONObject  and why we use it?
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.

krakatoaCommented:
is it java String?

Yes. Remember the '\ ' token is the escape character, to ensure that the double quote is not interpreted as a double quote in situ.
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
gudii9Author Commented:
On decoding, the default concrete class of java.util.List is org.json.simple.JSONArray and the default concrete class of java.util.Map is org.json.simple.JSONObject.
JSONArray is part of  java.util.List ?

and
JSONObject is part of java.util.Map?

are they newly added.
please advise
0
krakatoaCommented:
Well you can see right at the top of that code that JSONArray is imported, and so is a type which relies on the core API types, so no, it’s not “newly added”; you have to import it to use it.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

gudii9Author Commented:
what is meaning of json encode and decode means?
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);
      }
   }
}

Open in new window

in above program they are converting from java object to json object?
0
gudii9Author Commented:
https://www.javatpoint.com/java-json-example
i saw above examples as well.


I am confused what it means by encoding and what it means by decoding.
Encode means ake json object from java object?

Let's see a simple example to encode JSON object using map in java.

import java.util.HashMap;  
import java.util.Map;  
import org.json.simple.JSONValue;  
public class JsonExample2{    
public static void main(String args[]){    
  Map obj=new HashMap();    
  obj.put("name","sonoo");    
  obj.put("age",new Integer(27));    
  obj.put("salary",new Double(600000));   
  String jsonText = JSONValue.toJSONString(obj);  
  System.out.print(jsonText);  
}}    
Output:

{"name":"sonoo","salary":600000.0,"age":27}
Java JSON Array Encode

Let's see a simple example to encode JSON array in java.

import org.json.simple.JSONArray;  
public class JsonExample1{    
public static void main(String args[]){    
  JSONArray arr = new JSONArray();  
  arr.add("sonoo");    
  arr.add(new Integer(27));    
  arr.add(new Double(600000));   
  System.out.print(arr);  
}}    
Output:

["sonoo",27,600000.0]

Open in new window

why i got json output in above first example
where as
second example output is array?
0
krakatoaCommented:
I'm not up with Json so I have no idea what their custom classes do. But as I said, s is a String and in terms of Java when printed out, gives you  :

[0,{"1":{"2":{"3":{"4":[5,{"6":7}]}}}}]
0
gudii9Author Commented:
 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();

Open in new window

is it  second element of array?
below is not array right?
"[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
when they say array which one is array?

array usually looks as below 1D and 2D arrays
int arr[]={4,4,5};  
int a[][]={{1,3,4},{3,4,5}};  
int b[][]={{1,3,4},{3,4,5}};
0
krakatoaCommented:
I am going to paraphrase a comment that is easily Googlable here, but not link it, as it is anyway a generic statement :

Go to json.org and study the JSON syntax. It takes minutes to learn. In Java you can use Maps (to represent JSON "objects") and Lists (for JSON "arrays")
0
gudii9Author Commented:
package com.gp.json;

import org.json.simple.JSONObject;

public class SimplePlainJavaToJSONEncodeExample {
	public static void main(String args[]) {
		JSONObject obj = new JSONObject();
		obj.put("name", "sonoo");
		obj.put("age", new Integer(27));
		obj.put("salary", new Double(600000));
		System.out.print(obj);
	}
}

Open in new window

{"name":"sonoo","salary":600000.0,"age":27} //this is clear as they are just printing contents of json formed by jsonobject in above lines

package com.gp.json;

import java.util.HashMap;
import java.util.Map;
import org.json.simple.JSONValue;

public class JavaToJSONEncodeUsingMapExample {
	public static void main(String args[]) {
		Map obj = new HashMap();
		obj.put("name", "sonoo");
		obj.put("age", new Integer(27));
		obj.put("salary", new Double(600000));
		String jsonText = JSONValue.toJSONString(obj);
		System.out.print(jsonText);
	}
}

Open in new window


{"name":"sonoo","salary":600000.0,"age":27}//this output also clear as i got json object from java object map

package com.gp.json;

import java.util.ArrayList;
import java.util.List;
import org.json.simple.JSONValue;

public class JavaToJSONEncodeUsingListExample {
	public static void main(String args[]) {
		List arr = new ArrayList();
		arr.add("sonoo");
		arr.add(new Integer(27));
		arr.add(new Double(600000));
		String jsonText = JSONValue.toJSONString(arr);
		System.out.print(jsonText);
	}
}

Open in new window

["sonoo",27,600000.0] //not sure why we got array though?

import org.json.simple.JSONArray;  
public class JsonExample1{    
public static void main(String args[]){    
  JSONArray arr = new JSONArray();  
  arr.add("sonoo");    
  arr.add(new Integer(27));    
  arr.add(new Double(600000));   
  System.out.print(arr);  
}}  

Open in new window

Output:

["sonoo",27,600000.0]

//no clue why they used JSONARRAy not the java array to encode from java to json object??



package com.gp.json;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

public class JsonToJavaDecodeExample {
	public static void main(String[] args) {
		String s = "{\"name\":\"sonoo\",\"salary\":600000.0,\"age\":27}";
		Object obj = JSONValue.parse(s);
		JSONObject jsonObject = (JSONObject) obj;

		String name = (String) jsonObject.get("name");
		double salary = (Double) jsonObject.get("salary");
		long age = (Long) jsonObject.get("age");
		System.out.println(name + " " + salary + " " + age);
	}
}

Open in new window

sonoo 600000.0 27
// i  think they took json key value pairs as below line to convert that to java
String s = "{\"name\":\"sonoo\",\"salary\":600000.0,\"age\":27}";

please advise
0
krakatoaCommented:
The question should be revisited by the OP ideally. I do not feel my comments should be credited on this occasion, without the OP's endorsement, as I do not fully understand what the solution is he is seeking, or even what the problem is.
0
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
Java

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.