The jar package required in the project is not easy to find online, so I put it on my network disk and download it if necessary.
Click to download
1. Simple parsing json strings
First, convert the json string into a json object, and then parse the json object. The process is as follows.
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
Get its value based on 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, first convert the json string to a json object, and then convert the json object to a java object, as shown below.
JSONObject obj = new JSONObject().fromObject(jsonStr);//Convert json string to json object
Convert json object to java object
Person jb = (Person)JSONObject.toBean(obj,Person.class);//Convert the built json object to Person object
3. Convert java object to json string
First convert the java object to a json object, and then convert the json object to a json string
JSONObject json = JSONObject.fromObject(obj);//Convert java object to json object String str = json.toString();//Convert json object to 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; } /** * parse 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); } //Convert json string to java object public Person JSON2Object(){ //Receive {} objects, and there will be exceptions for receiving array objects here if(jsonStr.indexOf("[") != -1){ jsonStr = jsonStr.replace("[", ""); } if(jsonStr.indexOf("]") != -1){ jsonStr = jsonStr.replace("]", ""); } JSONObject obj = new JSONObject().fromObject(jsonStr);//Convert json string to json object Person jb = (Person)JSONObject.toBean(obj,Person.class);//Convert the built json object to Person object return jb;//Return 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; } } Convert java object to json string
package baz.cons; import net.sf.json.JSONObject; /** * Convert java object 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);//Convert java object to json object String str = json.toString();//Convert json object to string return str; } } 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) { //Convert a string to a json object, and then obtain the corresponding value according to the creation 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); } } 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 just the easiest way to use it, and I will update other uses in the later period. The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.