Conversion between json and javabean

Then the last http://www.cnblogs.com/ya-qiang/p/9009134.html essay, continue to introduce the conversion between json and java objects

First, the mutual conversion of java ordinary objects and json strings

java object---->json

First create a java object:

public class Student {
	//Name
	private String name;
	//age
	private String age;
	//address
	private String address;
	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 String getAddress() {
	return address;
	}
	public void setAddress(String address) {
	this.address = address;
	}
	@Override
	public String toString() {
	return "Student [name=" + name + ", age=" + age + ", address="
	+ address + "]";
	}
}

  Now the java object is converted to json form:

public static void convertObject() {
		Student stu=new Student();
		stu.setName("JSON");
		stu.setAge("23");
		stu.setAddress("Xicheng District, Beijing");
		//1. Use JSONObject
		JSONObject json = JSONObject.fromObject(stu);
		//2. Use JSONArray
		JSONArray array=JSONArray.fromObject(stu);
		String strJson=json.toString();
		String strArray=array.toString();
		System.out.println("strJson:"+strJson);
		System.out.println("strArray:"+strArray);
		}

  Define a Student entity class, and then use JSONObject and JSONArray to convert it into a JSON string respectively. See the printed result below:

json-->javabean

The above explains how to convert a java object into a JSON string. Let's see how to convert the JSON string format into a java object.

First, you need to define strings in two different formats, and you need to use \ to escape the double quotes.

public static void jsonStrToJava(){
		//Define two different formats of strings
		String objectStr="{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"北京市西城区\"}";
		String arrayStr="[{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"北京市西城区\"}]";
		//1. Use JSONObject
		JSONObject jsonObject=JSONObject.fromObject(objectStr);
		Student stu=(Student)JSONObject.toBean(jsonObject, Student.class);
		//2. Use JSONArray
		JSONArray jsonArray=JSONArray.fromObject(arrayStr);
		//Get the first element of jsonArray
		Object o=jsonArray.get(0);
		JSONObject jsonObject2=JSONObject.fromObject(o);
		Student stu2=(Student)JSONObject.toBean(jsonObject2, Student.class);
		System.out.println("stu:"+stu);
		System.out.println("stu2:"+stu2);
	}

  operation result:

As can be seen from the above code, using JSONObject can easily convert JSON-formatted strings into java objects, but using JSONArray is not so easy, because it has the "[]" symbol, so we get JSONArray here After the object, take its first element, which is the deformation of a student we need, and then use JSONObject to easily get it.

Second, the mutual conversion of list and json strings

The following converts the list to a json string:

public static void convertListObject() {
		Student stu=new Student();
		stu.setName("JSON");
		stu.setAge("23");
		stu.setAddress("Xicheng District, Beijing");
		Student stu2=new Student();
		stu2.setName("JSON2");
		stu2.setAge("23");
		stu2.setAddress("Xicheng District, Beijing");
		//Note that if it is a list of multiple objects than you need to use JSONArray
		JSONArray array=JSONArray.fromObject(stu);
		String strArray=array.toString();
		System.out.println("strArray:"+strArray);
	}

  The running result is: strArray:[{"address":"Xicheng District, Beijing","name":"JSON","age":"23"},{"address":"Xicheng District, Beijing","name ":"JSON2","age":"23"}]

If you use JSONObject for conversion, it will appear: Exception in thread "main" net.sf.json.JSONException: 'object' is an array. Use JSONArray instead

Convert the json string to list below

public static void jsonToList(){
		String arrayStr="[{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"北京市西城区\"},{\"name\":\"JSON2\",\"age\":\"24\",\"address\":\"北京市西城区\"}]";
		//convert to list
		List<Student> list2=(List<Student>)JSONArray.toList(JSONArray.fromObject(arrayStr), Student.class);
		for (Student stu : list2) {
		System.out.println(stu);
		}
		//convert to array
		Student[] ss =(Student[])JSONArray.toArray(JSONArray.fromObject(arrayStr),Student.class);
		for (Student student : ss) {
		System.out.println(student);
		}
	}

  The running result is:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325883748&siteId=291194637