Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

java json encode meaning

Java JSON Encode using Map

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]
Java JSON Array Encode using List

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

import java.util.ArrayList;  
import java.util.List;  
import org.json.simple.JSONValue;  
public class JsonExample1{    
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);  
}}    
Output:

["sonoo",27,600000.0]

Open in new window


what is meaning of encode?

what is difference of above 3 programs and outputs?

output1 from Java JSON Encode using Map is
{"name":"sonoo","salary":600000.0,"age":27}(why brackets are { } unlike [] etc??)

output2 from Java JSON Array Encode is
["sonoo",27,600000.0](why square brackets here??)

output3 from Java JSON Array Encode using List is
sonoo 600000.0 27 (why no brackets here??)
Avatar of David Favor
David Favor
Flag of United States of America image

This all relates to JSON formatting.

The questions you're asking relate to the JSON standard.

I suggest you read about JSON + then post any specific questions which remain.

Currently your questions as so broad, someone would have to break down the entire JSON system for you + explain it in detail.

The JSON docs are a better place for this.

https://en.wikipedia.org/wiki/JSON provides a good starting point.
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Girionis provided best answer.