JSON format, detailed description!

Source: Internet
Author: User
Tags string format

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
    1. Java.util.Date testdate = new Date ();
    2. Jsonobject jsonfromdate = Jsonobject.fromobject (testdate);
    3. System.out.println (jsonfromdate);
    4. 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
  1. Registering the conversion mode for date types
  2. Jsonconfig jsonconfig = new Jsonconfig ();
  3. Jsonconfig.registerjsonvalueprocessor (java.util.Date.   Class, new Jsonvalueprocessorimpltest ());
  4. Jsonobject Jsonfrombean = Jsonobject.fromobject (testbean,jsonconfig);
  5. System.out.println (Jsonfrombean);
  6. Prints {"Birthday": "2009-10-26", "id": "id", "name": "Name"}
  7. string[] Dateformats = new string[] {"yyyy/mm/dd","Yyyy-mm-dd"};
  8. Jsonutils.getmorpherregistry (). Registermorpher (new Datemorpher (dateformats));
  9. Testbean Jsontobean = (Testbean) Jsonobject.tobean (Jsonfrombean,testbean.    Class);
  10. System.out.println (Jsontobean);
  11. 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
  1. Import Java.util.Date;
  2. Import Org.apache.commons.lang.builder.ReflectionToStringBuilder;
  3. Public class Testbean {
  4. private String ID;
  5. private String name;
  6. private java.util.Date birthday;
  7. Public Testbean () {
  8. super ();
  9. }
  10. Public Testbean (string ID, string name, Date birthday) {
  11. super ();
  12. this.id = ID;
  13. this.name = name;
  14. this.birthday = birthday;
  15. }
  16. Public String getId () {
  17. return ID;
  18. }
  19. public void SetId (String id) {
  20. this.id = ID;
  21. }
  22. Public String GetName () {
  23. return name;
  24. }
  25. public void SetName (String name) {
  26. this.name = name;
  27. }
  28. Public java.util.Date Getbirthday () {
  29. return birthday;
  30. }
  31. public void Setbirthday (java.util.Date birthday) {
  32. this.birthday = birthday;
  33. }
  34. Public String toString () {
  35. return reflectiontostringbuilder.tostring (this);
  36. }
  37. }
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
  1. Import Java.text.SimpleDateFormat;
  2. Import Java.util.Date;
  3. Import Net.sf.json.JsonConfig;
  4. Import Net.sf.json.processors.JsonValueProcessor;
  5. Public class Jsonvalueprocessorimpltest implements Jsonvalueprocessor {
  6. private String format = "Yyyy-mm-dd";
  7. Public jsonvalueprocessorimpltest () {
  8. super ();
  9. }
  10. Public jsonvalueprocessorimpltest (String format) {
  11. super ();
  12. this.format = format;
  13. }
  14. @Override
  15. Public Object Processarrayvalue (object value, Jsonconfig jsonconfig) {
  16. string[] obj = {};
  17. if (value instanceof date[]) {
  18. SimpleDateFormat SF = new SimpleDateFormat (format);
  19. date[] dates = (date[]) value;
  20. obj = new String[dates.length];
  21. For (int i = 0; i < dates.length; i++) {
  22. Obj[i] = Sf.format (Dates[i]);
  23. }
  24. }
  25. return obj;
  26. }
  27. @Override
  28. Public Object Processobjectvalue (String key, object value, Jsonconfig jsonconfig) {
  29. if (value instanceof java.util.Date) {
  30. String str = new SimpleDateFormat (format). Format ((Date) value);
  31. return str;
  32. }
  33. return value.tostring ();
  34. }
  35. Public String GetFormat () {
  36. return format;
  37. }
  38. public void SetFormat (String format) {
  39. this.format = format;
  40. }
  41. }

JSON format, detailed description!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.