net.sf.json.JSONObject maven dependency

Keywords: JSON Java JDK REST

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);
  1. <pre></pre><span style="white-space:pre"></span>  
  2. <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:
  1. package baz.parse;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import net.sf.json.JSON;  
  7. import net.sf.json.JSONArray;  
  8. import net.sf.json.JSONObject;  
  9. import net.sf.json.JSONSerializer;  
  10. import baz.bean.Person;  
  11.   
  12. public class ParseJson {  
  13.       
  14.     private String jsonStr;  
  15.       
  16.     public ParseJson() {  
  17.           
  18.     }  
  19.       
  20.     public ParseJson(String str){  
  21.         this.jsonStr = str;  
  22.     }  
  23.     /** 
  24.      * Parsing json strings
  25.      */  
  26.     public void parse(){  
  27.         JSONObject jsonObject = JSONObject.fromObject(jsonStr);  
  28.         String name = jsonObject.getString("name");  
  29.         int num = jsonObject.getInt("num");  
  30.         String sex = jsonObject.getString("sex");  
  31.         int age = jsonObject.getInt("age");  
  32.           
  33.         System.out.println(name + " " + num + " " + sex + " " + age);  
  34.     }  
  35.     //Converting json strings to java objects
  36.     public Person JSON2Object(){  
  37.         //Receive {} objects, where receiving array objects will have exceptions.
  38.         if(jsonStr.indexOf("[") != -1){  
  39.             jsonStr = jsonStr.replace("[", "");  
  40.         }  
  41.         if(jsonStr.indexOf("]") != -1){  
  42.             jsonStr = jsonStr.replace("]", "");  
  43.         }  
  44.         JSONObject obj = new JSONObject().fromObject(jsonStr);//Convert json strings to json objects
  45.         Person jb = (Person)JSONObject.toBean(obj,Person.class);//Convert the build json object to Person object
  46.         return jb;//Returns a Person object.
  47.     }  
  48.       
  49.   
  50. }  
  1. package baz.bean;  
  2.   
  3. public class Person {  
  4.       
  5.     private String name;  
  6.     private int num;  
  7.     private String sex;  
  8.     private int age;  
  9.       
  10.     public Person() {  
  11.         // TODO Auto-generated constructor stub  
  12.     }  
  13.   
  14.     public Person(String name, int num, String sex, int age) {  
  15.         super();  
  16.         this.name = name;  
  17.         this.num = num;  
  18.         this.sex = sex;  
  19.         this.age = age;  
  20.     }  
  21.   
  22.   
  23.   
  24.     public String getName() {  
  25.         return name;  
  26.     }  
  27.   
  28.     public void setName(String name) {  
  29.         this.name = name;  
  30.     }  
  31.   
  32.     public int getNum() {  
  33.         return num;  
  34.     }  
  35.   
  36.     public void setNum(int num) {  
  37.         this.num = num;  
  38.     }  
  39.   
  40.     public String getSex() {  
  41.         return sex;  
  42.     }  
  43.   
  44.     public void setSex(String sex) {  
  45.         this.sex = sex;  
  46.     }  
  47.   
  48.     public int getAge() {  
  49.         return age;  
  50.     }  
  51.   
  52.     public void setAge(int age) {  
  53.         this.age = age;  
  54.     }  
  55.       
  56. }  
Converting java objects to json strings
 
  1. package baz.cons;  
  2.   
  3.   
  4. import net.sf.json.JSONObject;  
  5.   
  6.   
  7. /** 
  8.  * Converting java objects to json strings
  9.  * @author Administrator 
  10.  * 
  11.  */  
  12. public class ConsJson {  
  13.       
  14.     public ConsJson() {  
  15.         // TODO Auto-generated constructor stub  
  16.     }  
  17.       
  18.     public String Object2Json(Object obj){  
  19.         JSONObject json = JSONObject.fromObject(obj);//Converting java objects to json objects
  20.         String str = json.toString();//Converting json objects to strings
  21.           
  22.         return str;  
  23.     }  
  24. }  
Test class:
  1. package baz.test;  
  2.   
  3. import java.util.List;  
  4.   
  5. import baz.bean.Person;  
  6. import baz.cons.ConsJson;  
  7. import baz.parse.ParseJson;  
  8.   
  9.   
  10. public class Test {  
  11.     public static void main(String[] args) {  
  12.           
  13.         //Convert the string to a json object, and then get the corresponding value based on the construction.
  14.         ParseJson pj = new ParseJson("{\"name\":\"gu\",\"num\":123456,\"sex\":\"male\",\"age\":24}");  
  15.         pj.parse();  
  16.           
  17.         //Convert a json string to a java object
  18.         Person p = pj.JSON2Object();  
  19.         System.out.println("Name:" + p.getName());  
  20.         System.out.println("Num:" + p.getNum());  
  21.         System.out.println("Sex:" + p.getSex());  
  22.         System.out.println("age:" + p.getAge());  
  23.           
  24.         //Convert a java object to a Json string
  25.         Person p1 = new Person("gu1",123,"male",23);  
  26.         ConsJson cj = new ConsJson();  
  27.         String str1 = cj.Object2Json(p1);  
  28.         System.out.println(str1);  
  29.           
  30.     }  
  31.   
  32. }  
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"}
This is the easiest way to use it. I will update the rest later.
I'm just a beginner. Welcome to Chivalry!!

Posted by neodaemon on Wed, 06 Feb 2019 04:09:18 -0800