Method of interconversion between java objects and json objects

  • 2020-05-27 05:31:34
  • OfStack

The jar package needed in the project is not easy to find on the Internet, so I put it in my network disk, and download it if necessary.

Click on the download

1. Simply parse the json string

First, the json string is converted to an json object, and then the json object is parsed as follows.


JSONObject jsonObject = JSONObject.fromObject(jsonStr);

Get its value according to 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. Convert json string to java object

Also convert the json string to the json object, and then the json object to the java object, as shown below.


JSONObject obj = new JSONObject().fromObject(jsonStr);
// will json String conversion to json object 

Convert an json object to an java object


Person jb = (Person)JSONObject.toBean(obj,Person.class);
// Will be built json Object conversion 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);

// will java Object conversion to json object 
String str = json.toString();// will json Object to a string 

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 string  
   */ 
  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); 
  } 
  // will json String conversion to java object  
  public Person JSON2Object(){ 
    // receive {} Object, where the receiving array object will have an exception  
    if(jsonStr.indexOf("[") != -1){ 
      jsonStr = jsonStr.replace("[", ""); 
    } 
    if(jsonStr.indexOf("]") != -1){ 
      jsonStr = jsonStr.replace("]", ""); 
    } 
    JSONObject obj = new JSONObject().fromObject(jsonStr);// will json String conversion to json object  
    Person jb = (Person)JSONObject.toBean(obj,Person.class);// Will be built json Object conversion to Person object  
    return jb;// return 1 a Person object  
  } 
   
 
} 

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; 
  } 
   
} 

Converts the java object to an json string


package baz.cons; 
 import net.sf.json.JSONObject; 
 /** 
 *  will java Object conversion to json string  
 * @author Administrator 
 * 
 */ 
public class ConsJson { 
   
  public ConsJson() { 
    // TODO Auto-generated constructor stub 
  } 
   
  public String Object2Json(Object obj){ 
    JSONObject json = JSONObject.fromObject(obj);// will java Object conversion to json object  
    String str = json.toString();// will json Object to a string  
     
    return str; 
  } 
} 

The test class:


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) { 
     
    // Converts the string to json Object, and then get the corresponding value according to the building  
    ParseJson pj = new ParseJson("{\"name\":\"gu\",\"num\":123456,\"sex\":\"male\",\"age\":24}"); 
    pj.parse(); 
     
    // will 1 a json String conversion to 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()); 
     
    // will 1 a java Object conversion to Json string  
    Person p1 = new Person("gu1",123,"male",23); 
    ConsJson cj = new ConsJson(); 
    String str1 = cj.Object2Json(p1); 
    System.out.println(str1); 
     
  } 
 
} 

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"}

Related articles: