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??)
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.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
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.
0
girionisCommented:
> what is meaning of encode?

Encode (in this context) simply means "create a JSON string in Java"

> what is difference of above 3 programs and outputs?

In the first example you output a JSON string with three variables, and their corresponding values. In the 2nd & 3rd example you output a JSON string as an array (no variable names, just the values). Encoding a JSON array or list is the same.
1

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
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Girionis provided best answer.
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
Web Development

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.