Json <---> List set, mutual conversion between entity classes, and mutual conversion between json
Package com. hp. svse; import java. text. simpleDateFormat; import java. util. date; import java. util. locale; import net. sf. json. jsonConfig; import net. sf. json. processors. jsonValueProcessor;/*** Timestamp processor */public class JsonDateValueProcessor implements JsonValueProcessor {private String formatDateTime = "yyyy-MM-dd HH: mm: ss "; private String formatDate = "yyyy-MM-dd"; public Object processArrayValue (Objec T value, JsonConfig config) {return process (value);} public Object processObjectValue (String key, Object value, JsonConfig config) {return process (value );} private Object process (Object value) {if (value instanceof Date) {SimpleDateFormat sdf; if (value. toString (). length () <= 11 | value. toString (). indexOf ("00:00:00. 0 ")> = 0) {sdf = new SimpleDateFormat (formatDate, Locale. UK);} else {sdf = new SimpleDat EFormat (formatDateTime, Locale. UK);} return sdf. format (value);} return value = null? "": Value. toString ();}}
Package com. hp. svse; import java. util. arrayList; import java. util. date; import java. util. list; import net. sf. json. JSONArray; import net. sf. json. JSONObject; import net. sf. json. jsonConfig; public class JsonTest {public static void main (String [] args) {/*** convert an object set to a JSON string */List <Student> students = new ArrayList <Student> (); students. add (new Student ("James 1", "male", "Hubei", "SVSE"); students. add (new Student ("James 2", "female", "Guangdong", "GIS"); students. add (new Student ("James 3", "male", "Hong Kong", "3G"); JsonTest jsonTest = new JsonTest (); String json = jsonTest. beanListToJSON (students); System. out. println (json);/*** output result: * [* {"sex": "male", "address": "Hubei", "stuname ": "James 1", "classname": "SVSE", "smallStudents": []}, * {"sex": "female", "address": "Guangdong ", "stuname": "James 2", "classname": "GIS", "smallStudents": []}, * {"sex": "male", "address ": "Hong Kong", "Stuname": "James 3", "classname": "3G", "smallStudents ": []} *] * // *** converts a json String to a json object and then converts a json object to an object bean */String bookCotentjsonStr = "{\" sex \": \ "male \", \ "address \": \ "Hubei \", \ "stuname \": \ "Xiaoming 1 \", \ "classname \": \ "SVSE \"} "; JSONObject bookCotentjson = JSONObject. fromObject (bookCotentjsonStr); Student student = (Student) JSONObject. toBean (bookCotentjson, Student. class); System. out. println (student);/*** enter Result: Student [address = Hubei, classname = SVSE, sex = male, stuname = James 1] * // *** converts an object to a JSON string and applies to a single object (a single object can contain List <T>) */List <SmallStudent> smallStudents = new ArrayList <SmallStudent> (); for (int I = 0; I <3; I ++) {SmallStudent smallStudent = new SmallStudent (); smallStudent. setName ("pupils" + I); smallStudent. setAge ("1" + I); smallStudents. add (smallStudent);} Student student2 = new Student ("James", "male "," Wuhan "," svse ", smallStudents); String jsonStr = jsonTest. beanToJSON (student2); System. out. println (jsonStr);/*** output: * {"sex": "male", "address": "Wuhan", "stuname": "James ", "classname": "svse", "smallStudents": [{"sex": "", "age": "10", "name": "primary school student 0 "}, {"sex": "", "age": "11", "name": "pupils 1" },{ "sex": "", "age ": "12", "name ": "Elementary School 2"}]} */}/*** converts the set into a JSON String */public <T> String beanListToJSON (List <T> t) {String json = ""; JsonConfig jsonConfig = new JsonConfig (); jsonConfig. registerJsonValueProcessor (Date. class, new JsonDateValueProcessor (); if (t! = Null) {json = JSONArray. fromObject (t, jsonConfig ). toString ();} else {json = "[]";} return json ;} /*** converts an object to a String in JSON format. It is applicable to conversion of a single object (a single object can contain List <T>) */public <T> String beanToJSON (T t) {String json = ""; JsonConfig jsonConfig = new JsonConfig (); jsonConfig. registerJsonValueProcessor (Date. class, new JsonDateValueProcessor (); if (t! = Null) {json = JSONObject. fromObject (t, jsonConfig). toString () ;}else {json = "[]" ;}return json ;}}
package com.hp.svse;import java.util.List;public class Student { private String stuname; private String sex; private String address; private String classname; private List<SmallStudent> smallStudents; public Student(String stuname, String sex, String address, String classname,List<SmallStudent> smallStudents) { super(); this.stuname = stuname; this.sex = sex; this.address = address; this.classname = classname; this.smallStudents = smallStudents; } public Student(String stuname, String sex, String address, String classname) { super(); this.stuname = stuname; this.sex = sex; this.address = address; this.classname = classname; } public Student() { super(); } public String getStuname() { return stuname; } public void setStuname(String stuname) { this.stuname = stuname; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getClassname() { return classname; } public void setClassname(String classname) { this.classname = classname; } public List<SmallStudent> getSmallStudents() { return smallStudents; } public void setSmallStudents(List<SmallStudent> smallStudents) { this.smallStudents = smallStudents; } @Override public String toString() { return "Student [address=" + address + ", classname=" + classname + ", sex=" + sex + ", stuname=" + stuname + "]"; } }
package com.hp.svse;public class SmallStudent { private String name; private String age; private String sex; 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 getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "SmallStudent [age=" + age + ", name=" + name + ", sex=" + sex + "]"; }}