For the date type of conversion has always been a more complex JSON, I just put forward a way I will share with you, if it's better, don't forget to tell me how to learn the next hey in here first thanks
For the date type, direct conversions are as follows
Java code
- Java.util.Date testdate = new Date ();
- Jsonobject jsonfromdate = Jsonobject.fromobject (testdate);
- System.out.println (jsonfromdate);
- Prints {"Date": +, "Day": 1, "hours": one, "minutes": +, "month": 9, "Seconds": "Time": 1256527818296, "Timezoneoffset ": -480," Year ": 109}
Java.util.Date testdate = new Date (); Jsonobject jsonfromdate = Jsonobject.fromobject (testdate); System.out.println (jsonfromdate);//prints {"Date": +, "Day": 1, "hours": one, "minutes":--"Month": 9, "seconds": 18, " Time ": 1256527818296," Timezoneoffset ": -480," Year ": 109}
The above converted format is obviously detrimental to our use, the following describes my approach
Java code
- Registering the conversion mode for date types
- Jsonconfig jsonconfig = new Jsonconfig ();
- Jsonconfig.registerjsonvalueprocessor (java.util.Date. Class, new Jsonvalueprocessorimpltest ());
- Jsonobject Jsonfrombean = Jsonobject.fromobject (testbean,jsonconfig);
- System.out.println (Jsonfrombean);
- Prints {"Birthday": "2009-10-26", "id": "id", "name": "Name"}
- string[] Dateformats = new string[] {"yyyy/mm/dd","Yyyy-mm-dd"};
- Jsonutils.getmorpherregistry (). Registermorpher (new Datemorpher (dateformats));
- Testbean Jsontobean = (Testbean) Jsonobject.tobean (Jsonfrombean,testbean. Class);
- System.out.println (Jsontobean);
- Prints [email protected][id=id,name=name,birthday=mon Oct 00:00:00 CST 2009]
Register the conversion mode of date type Jsonconfig jsonconfig = new Jsonconfig (); Jsonconfig.registerjsonvalueprocessor (Java.util.Date.class, New Jsonvalueprocessorimpltest ()); Jsonobject Jsonfrombean = Jsonobject.fromobject (testbean,jsonconfig); System.out.println (Jsonfrombean);//prints {"Birthday": "2009-10-26", "id": "id", "name": "Name"}string[] Dateformats = New string[] {"Yyyy/mm/dd", "Yyyy-mm-dd"}; Jsonutils.getmorpherregistry (). Registermorpher (New Datemorpher (dateformats)); Testbean Jsontobean = (Testbean) Jsonobject.tobean (Jsonfrombean,testbean.class); System.out.println (Jsontobean);//prints [email Protected][id=id,name=name,birthday=mon Oct 00:00:00 CST 2009]
The following classes are required:
1. Prepare test data
Java code
- Import Java.util.Date;
- Import Org.apache.commons.lang.builder.ReflectionToStringBuilder;
- Public class Testbean {
- private String ID;
- private String name;
- private java.util.Date birthday;
- Public Testbean () {
- super ();
- }
- Public Testbean (string ID, string name, Date birthday) {
- super ();
- this.id = ID;
- this.name = name;
- this.birthday = birthday;
- }
- Public String getId () {
- return ID;
- }
- public void SetId (String id) {
- this.id = ID;
- }
- Public String GetName () {
- return name;
- }
- public void SetName (String name) {
- this.name = name;
- }
- Public java.util.Date Getbirthday () {
- return birthday;
- }
- public void Setbirthday (java.util.Date birthday) {
- this.birthday = birthday;
- }
- Public String toString () {
- return reflectiontostringbuilder.tostring (this);
- }
- }
Import Java.util.date;import Org.apache.commons.lang.builder.reflectiontostringbuilder;public class TestBean { private string Id;private string name;private java.util.Date birthday;public Testbean () {super ();} Public Testbean (string ID, string name, Date birthday) {super (); this.id = Id;this.name = Name;this.birthday = Birthday;} Public String GetId () {return ID;} public void SetId (String id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Java.util.Date Getbirthday () {return birthday;} public void Setbirthday (java.util.Date birthday) {this.birthday = birthday;} Public String toString () {return reflectiontostringbuilder.tostring (this);}}
2. Create a Date format class
Java code
- Import Java.text.SimpleDateFormat;
- Import Java.util.Date;
- Import Net.sf.json.JsonConfig;
- Import Net.sf.json.processors.JsonValueProcessor;
- Public class Jsonvalueprocessorimpltest implements Jsonvalueprocessor {
- private String format = "Yyyy-mm-dd";
- Public jsonvalueprocessorimpltest () {
- super ();
- }
- Public jsonvalueprocessorimpltest (String format) {
- super ();
- this.format = format;
- }
- @Override
- Public Object Processarrayvalue (object value, Jsonconfig jsonconfig) {
- string[] obj = {};
- if (value instanceof date[]) {
- SimpleDateFormat SF = new SimpleDateFormat (format);
- date[] dates = (date[]) value;
- obj = new String[dates.length];
- For (int i = 0; i < dates.length; i++) {
- Obj[i] = Sf.format (Dates[i]);
- }
- }
- return obj;
- }
- @Override
- Public Object Processobjectvalue (String key, object value, Jsonconfig jsonconfig) {
- if (value instanceof java.util.Date) {
- String str = new SimpleDateFormat (format). Format ((Date) value);
- return str;
- }
- return value.tostring ();
- }
- Public String GetFormat () {
- return format;
- }
- public void SetFormat (String format) {
- this.format = format;
- }
- }
JSON format, detailed description!