The last line needs to be retained. There are two JDK versions: json-lib-2.1-jdk13.jar and json-lib-2.1-jdk15.jar.
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
1. Simple parsing of json strings
Firstly, the json string is converted into a json object, and then the json object is parsed. The procedure is as follows.
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
- <pre></pre><span style="white-space:pre"></span>
- <pre></pre>
Get its value from the key in json
String name = jsonObject.getString("name");
int num = jsonObject.getInt("num");
String sex = jsonObject.getString("sex");
int age = jsonObject.getInt("age");
2. Converting json strings to java objects
Similarly, json strings are converted to json objects, and then json objects are converted to java objects, as shown below.
JSONObject obj = new JSONObject().fromObject(jsonStr); // Convert json strings to json objects
Converting json objects to java objects
Person jb = (Person)JSONObject.toBean(obj,Person.class); // Convert the constructed json object to Person object
3. Convert java objects to json strings
First convert java object to json object, then convert json object to json string
JSONObject json = JSONObject.fromObject(obj); // Convert java objects to json objects
String str = json.toString(); // Converting JSON objects to strings
The complete code is as follows:
- package baz.parse;
- import java.util.ArrayList;
- import java.util.List;
- import net.sf.json.JSON;
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
- import net.sf.json.JSONSerializer;
- import baz.bean.Person;
- public class ParseJson {
- private String jsonStr;
- public ParseJson() {
- }
- public ParseJson(String str){
- this.jsonStr = str;
- }
- /**
- * Parsing json strings
- */
- public void parse(){
- JSONObject jsonObject = JSONObject.fromObject(jsonStr);
- String name = jsonObject.getString("name");
- int num = jsonObject.getInt("num");
- String sex = jsonObject.getString("sex");
- int age = jsonObject.getInt("age");
- System.out.println(name + " " + num + " " + sex + " " + age);
- }
- //Converting json strings to java objects
- public Person JSON2Object(){
- //Receive {} objects, where receiving array objects will have exceptions.
- if(jsonStr.indexOf("[") != -1){
- jsonStr = jsonStr.replace("[", "");
- }
- if(jsonStr.indexOf("]") != -1){
- jsonStr = jsonStr.replace("]", "");
- }
- JSONObject obj = new JSONObject().fromObject(jsonStr);//Convert json strings to json objects
- Person jb = (Person)JSONObject.toBean(obj,Person.class);//Convert the build json object to Person object
- return jb;//Returns a Person object.
- }
- }
Converting java objects to json strings
- package baz.bean;
- public class Person {
- private String name;
- private int num;
- private String sex;
- private int age;
- public Person() {
- // TODO Auto-generated constructor stub
- }
- public Person(String name, int num, String sex, int age) {
- super();
- this.name = name;
- this.num = num;
- this.sex = sex;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getNum() {
- return num;
- }
- public void setNum(int num) {
- this.num = num;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
Test class:
- package baz.cons;
- import net.sf.json.JSONObject;
- /**
- * Converting java objects to json strings
- * @author Administrator
- *
- */
- public class ConsJson {
- public ConsJson() {
- // TODO Auto-generated constructor stub
- }
- public String Object2Json(Object obj){
- JSONObject json = JSONObject.fromObject(obj);//Converting java objects to json objects
- String str = json.toString();//Converting json objects to strings
- return str;
- }
- }
The test output is as follows: gu 123456 male 24 Name:gu Num:123456 Sex:male age:24 {"age":23,"name":"gu1","num":123,"sex":"male"}
- package baz.test;
- import java.util.List;
- import baz.bean.Person;
- import baz.cons.ConsJson;
- import baz.parse.ParseJson;
- public class Test {
- public static void main(String[] args) {
- //Convert the string to a json object, and then get the corresponding value based on the construction.
- ParseJson pj = new ParseJson("{\"name\":\"gu\",\"num\":123456,\"sex\":\"male\",\"age\":24}");
- pj.parse();
- //Convert a json string to a java object
- Person p = pj.JSON2Object();
- System.out.println("Name:" + p.getName());
- System.out.println("Num:" + p.getNum());
- System.out.println("Sex:" + p.getSex());
- System.out.println("age:" + p.getAge());
- //Convert a java object to a Json string
- Person p1 = new Person("gu1",123,"male",23);
- ConsJson cj = new ConsJson();
- String str1 = cj.Object2Json(p1);
- System.out.println(str1);
- }
- }
This is the easiest way to use it. I will update the rest later.
I'm just a beginner. Welcome to Chivalry!!