Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Java JSON Example


The json.simple library allows us to read and write JSON data in Java. In other words, we can encode and decode JSON object in java using json.simple library.

The org.json.simple package contains important classes for JSON API.

  • JSONValue
  • JSONObject
  • JSONArray
  • JsonString
  • JsonNumber

Install json.simple

To install json.simple, you need to set classpath of json-simple.jar or add the Maven dependency.

1) Download json-simple.jar, Or

2) To add maven dependency, write the following code in pom.xml file.

  1. <dependency>
  2.     <groupId>com.googlecode.json-simple</groupId>
  3.     <artifactId>json-simple</artifactId>
  4.     <version>1.1</version>
  5.   </dependency>

1) Java JSON Encode

Let’s see a simple example to encode JSON object in java.

  1. import org.json.simple.JSONObject;
  2. public class JsonExample1{
  3. public static void main(String args[]){
  4. JSONObject obj=new JSONObject();
  5.   obj.put(“name”,“sonoo”);
  6.   obj.put(“age”,new Integer(27));
  7.   obj.put(“salary”,new Double(600000));
  8.    System.out.print(obj);
  9. }}

Output:

{"name":"sonoo","salary":600000.0,"age":27}

Java JSON Encode using Map

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

  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import org.json.simple.JSONValue;
  4. public class JsonExample2{
  5. public static void main(String args[]){
  6.   Map obj=new HashMap();
  7.   obj.put(“name”,“sonoo”);
  8.   obj.put(“age”,new Integer(27));
  9.   obj.put(“salary”,new Double(600000));
  10.   String jsonText = JSONValue.toJSONString(obj);
  11.   System.out.print(jsonText);
  12. }}

Output:

{"name":"sonoo","salary":600000.0,"age":27}

Java JSON Array Encode

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

  1. import org.json.simple.JSONArray;
  2. public class JsonExample1{
  3. public static void main(String args[]){
  4.   JSONArray arr = new JSONArray();
  5.   arr.add(“sonoo”);
  6.   arr.add(new Integer(27));
  7.   arr.add(new Double(600000));
  8.   System.out.print(arr);
  9. }}

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.

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import org.json.simple.JSONValue;
  4. public class JsonExample1{
  5. public static void main(String args[]){
  6.   List arr = new ArrayList();
  7.   arr.add(“sonoo”);
  8.   arr.add(new Integer(27));
  9.   arr.add(new Double(600000));
  10.   String jsonText = JSONValue.toJSONString(arr);
  11.   System.out.print(jsonText);
  12. }}

Output:

["sonoo",27,600000.0]

2) Java JSON Decode

Let’s see a simple example to decode JSON string in java.

  1. import org.json.simple.JSONObject;
  2. import org.json.simple.JSONValue;
  3. public class JsonDecodeExample1 {
  4. public static void main(String[] args) {
  5.     String s=“{\”name\”:\”sonoo\”,\”salary\”:600000.0,\”age\”:27}”;
  6.     Object obj=JSONValue.parse(s);
  7.     JSONObject jsonObject = (JSONObject) obj;
  8.     String name = (String) jsonObject.get(“name”);
  9.     double salary = (Double) jsonObject.get(“salary”);
  10.     long age = (Long) jsonObject.get(“age”);
  11.     System.out.println(name+” “+salary+” “+age);
  12. }
  13. }

Output:

sonoo 600000.0 27

Comments

comments