• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 103
  • Last Modified:

json encoding

import org.json.simple.JSONObject;

class JsonEncodeDemo {

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

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

      System.out.print(obj);
   }
}
On compiling and executing the above program the following result will be generated −

{"balance": 1000.21, "num":100, "is_vip":true, "name":"foo"}

Open in new window


what above example doing
is it encoding jsonobject to java object?
i see output also seems like json to me?
Following is another example that shows a JSON object streaming using Java JSONObject −

import org.json.simple.JSONObject;

class JsonEncodeDemo {

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

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

      StringWriter out = new StringWriter();
      obj.writeJSONString(out);
      
      String jsonText = out.toString();
      System.out.print(jsonText);
   }
}
On compiling and executing the above program, the following result is generated −

{"balance": 1000.21, "num":100, "is_vip":true, "name":"foo"}

Open in new window


how above one is differen from first one?
0
gudii9
Asked:
gudii9
  • 2
  • 2
1 Solution
 
Julian HansenCommented:
is it encoding jsonobject to java object?
It is creating JSON output. It uses the JSONObject class to add key / value pairs to an internal memory representation of the data. It then creates a JSON string which it outputs

Both samples do the same thing - they just use slightly different approaches
0
 
gudii9Author Commented:
what is the difference of approaches?

below is mapping right what it represents in json and what corresponding represention in java? JsonArray used wherever java List used and vice versa similarly JsonArray used wherever JAva Map is used?

JSON      Java
string      java.lang.String
number      java.lang.Number
true|false      java.lang.Boolean
null      null
array      java.util.List
object      java.util.Map
0
 
Julian HansenCommented:
The only difference is in how they are displayed.
The first example shows how you can output the Json String by printing it, the second demonstrates how you can send the JSON string to a StringWriter using the writeJSONString() method.

The examples don't really do anything other than construct an JSON object and dump the string.
0
 
gudii9Author Commented:
i saw one similar example which just builds json with regular data and then array data and just prints
https://www.mkyong.com/java/json-simple-example-read-and-write-json/
0
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now