Maven Dependency:
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
Student Pojo:
package com.ram.json.simple.pojo;
import java.util.ArrayList;
import java.util.List;
public class Student {
public String name;
public String age;
public List<String> list = new ArrayList<String>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
Convert Java To JSON:
package com.ram.json.simple;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.json.simple.JSONObject;
import com.ram.jackson.pojo.Student;
public class JavaToJson {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Student student = new Student();
student.setName("Ramakrishna");
student.setAge("26");
List<String> list = new ArrayList<String>();
list.add("100");
list.add("200");
list.add("300");
student.setList(list);
JSONObject jsonObject = new JSONObject();
jsonObject.put("Name", student.getName());
jsonObject.put("Age", student.getAge());
jsonObject.put("list", student.getList());
try {
FileWriter file = new FileWriter("/home/biton/ramakrishna/delete/json/student.json");
file.write(jsonObject.toJSONString());
file.flush();
file.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.print(jsonObject);
}
}
Convert JSON To Java:
package com.ram.json.simple;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JsonToJava {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
Object object = parser.parse(new FileReader("/home/biton/ramakrishna/delete/json/student.json"));
JSONObject jsonObject = (JSONObject) object;
System.out.println(jsonObject);
String name = (String) jsonObject.get("Name");
System.out.println(name);
String age = (String) jsonObject.get("Age");
System.out.println(age);
List<String> list = (List<String>) jsonObject.get("list");
System.out.println(list);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
Student Pojo:
package com.ram.json.simple.pojo;
import java.util.ArrayList;
import java.util.List;
public class Student {
public String name;
public String age;
public List<String> list = new ArrayList<String>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
Convert Java To JSON:
package com.ram.json.simple;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.json.simple.JSONObject;
import com.ram.jackson.pojo.Student;
public class JavaToJson {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Student student = new Student();
student.setName("Ramakrishna");
student.setAge("26");
List<String> list = new ArrayList<String>();
list.add("100");
list.add("200");
list.add("300");
student.setList(list);
JSONObject jsonObject = new JSONObject();
jsonObject.put("Name", student.getName());
jsonObject.put("Age", student.getAge());
jsonObject.put("list", student.getList());
try {
FileWriter file = new FileWriter("/home/biton/ramakrishna/delete/json/student.json");
file.write(jsonObject.toJSONString());
file.flush();
file.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.print(jsonObject);
}
}
Convert JSON To Java:
package com.ram.json.simple;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JsonToJava {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
Object object = parser.parse(new FileReader("/home/biton/ramakrishna/delete/json/student.json"));
JSONObject jsonObject = (JSONObject) object;
System.out.println(jsonObject);
String name = (String) jsonObject.get("Name");
System.out.println(name);
String age = (String) jsonObject.get("Age");
System.out.println(age);
List<String> list = (List<String>) jsonObject.get("list");
System.out.println(list);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}