json encoding

gudii9
gudii9 used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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

Author

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
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
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.

Author

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/

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial