JSON – JavaScript Object Notation
JSON is a simple data exchange format for read and write data.JSON is the best alternative for XML data.We can create JSON data using Jackson, Google Gson, JSON.simple libraries.
In this tutorials we are going to see how to create json data using JSONSimple jar.
Create project called JSONExample.
Create package called com.javatutorialscorner.json
Create java class called JSONExample under com.javatutorialscorner.json.
JSONExample.java
package com.javatutorialscorner.json;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class JSONExample {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JSONObject json = new JSONObject();
json.put("name", "www.javatutorialscorner.com");
json.put("Tutorials", "Java & Related Technologies");
JSONArray jsArray = new JSONArray();
jsArray.add("Java");
jsArray.add("J2EE");
jsArray.add("Servlet");
jsArray.add("JSP");
jsArray.add("JSF");
jsArray.add("Struts");
jsArray.add("Spring");
jsArray.add("Hibernate");
json.put("Topics", jsArray);
System.out.println("Json Object " + json);
System.out.println("Json String " + json.toJSONString());
System.out.println("To String " + json.toString());
System.out.println("JSON get " + json.get("Tutorials"));
}
}
Now run the program see the following output in console.
Json Object {"Tutorials":"Java & Related Technologies","name":"www.javatutorialscorner.com","Topics":["Java","J2EE","Servlet","JSP","JSF","Struts","Spring","Hibernate"]}
Json String {"Tutorials":"Java & Related Technologies","name":"www.javatutorialscorner.com","Topics":["Java","J2EE","Servlet","JSP","JSF","Struts","Spring","Hibernate"]}
To String {"Tutorials":"Java & Related Technologies","name":"www.javatutorialscorner.com","Topics":["Java","J2EE","Servlet","JSP","JSF","Struts","Spring","Hibernate"]}
JSON get Java & Related Technologies
0 comments:
Post a Comment