The perfect solution for JSON date format transfer between Java and C #

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/wilsonke/article/details/24362851

The role of a simple and convenient data transmission scheme, JSON has become an alternative to XML fact standard. However, in JSON, the time (datetime,timestamp,date, etc.) format has not been well-integrated, and when cross-platform serialization/deserialization is required, there is a lot of trouble. The author has repeatedly tried to solve the difficulty of C # and Java time transmission through JSON.

C # parsing Java/javascript generated JSON is not difficult, but Java parsing C # generated JSON is difficult. The following focuses on this issue.

1. Basic situation

Java side:
Java-side commonly used Json-lib library does not support the timestamp type of deserialization (some say you can change the data type Ah, sorry, many of the historical code is not to change it can be changed). and Jackson and Gson can support deserialization in the form of milliseconds. It should be said that the serialization behavior of Java is relatively simple and commonsense.

C # side:
By default, C # time will be formatted as "/date (1294499956278+0800)/" in this form. Obviously, this form is difficult to deal with.

Fortunately, the JsonConvert third-party library provides two additional formats:

A. Isodatetimeconverter

C # code
    1. Isodatetimeconverter convert = new Isodatetimeconverter ();
    2. string ret = Jsonconvert.serializeobject (bean, formatting.none, convert);
[C #]View Plaincopy
    1. Isodatetimeconverter convert = new Isodatetimeconverter ();
    2. string ret = Jsonconvert.serializeobject (bean, formatting.none, convert);


The result of formatting using this method is
"2013-05-31t15:14:13.1294788+08:00"

B. Javascriptdatetimeconverter

C # code
    1. Javascriptdatetimeconverter convert = new Javascriptdatetimeconverter ();
    2. string ret = Jsonconvert.serializeobject (bean, formatting.none, convert);
[C #]View Plaincopy
    1. Javascriptdatetimeconverter convert = new Javascriptdatetimeconverter ();
    2. string ret = Jsonconvert.serializeobject (bean, formatting.none, convert);


The result of formatting using this method is
New Date (1369984667554)

However, even so, there is no one of the three formats of C # that is the same as Java and still cannot be properly docked.

2. Solutions

After repeated testing, the final decision is to use "Yyyy-mm-dd HH:mm:ss" on both ends. SSS "format for transmission, which is the only scenario that has been successfully tested so far.

Java side:

Java code
  1. Jsongenerator jsongenerator = null;
  2. Objectmapper objectmapper = null;
  3. Objectmapper = new Objectmapper ();
  4. SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss.   SSS ");
  5. Objectmapper.getdeserializationconfig (). Setdateformat (formatter);
  6. try {
  7. Jsongenerator = Objectmapper.getjsonfactory (). Createjsongenerator (System.out, Jsonencoding.utf8);
  8. } catch (IOException e) {
  9. E.printstacktrace ();
  10. }
  11. String json = "{\" name\ ": \" Yk\ ", \" value\ ": 3,\" tm\ ": \" 2013-05-31 02:53:20.123\ "}";
  12. try {
  13. Mybean B = Objectmapper.readvalue (JSON, Mybean.   Class);
  14. System.out.println (B.gettm ());
  15. } catch (Exception e) {
  16. E.printstacktrace ();
  17. }
[Java]View Plaincopy
  1. Jsongenerator jsongenerator = null;
  2. Objectmapper objectmapper = null;
  3. Objectmapper = new Objectmapper ();
  4. SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss.   SSS ");
  5. Objectmapper.getdeserializationconfig (). Setdateformat (formatter);
  6. try {
  7. Jsongenerator = Objectmapper.getjsonfactory (). Createjsongenerator (System.out, Jsonencoding.utf8);
  8. } catch (IOException e) {
  9. E.printstacktrace ();
  10. }
  11. String json = "{\" name\ ": \" Yk\ ", \" value\ ": 3,\" tm\ ": \" 2013-05-31 02:53:20.123\ "}";
  12. try {
  13. Mybean B = Objectmapper.readvalue (JSON, Mybean.   Class);
  14. System.out.println (B.gettm ());
  15. } catch (Exception e) {
  16. E.printstacktrace ();
  17. }


C # code
    1. Isodatetimeconverter convert = new Isodatetimeconverter ();
    2. Convert.  DateTimeFormat = "Yyyy-mm-dd HH:mm:ss.fff";
    3. string ret = Jsonconvert.serializeobject (bean, formatting.none, convert);
[C #]View Plaincopy
    1. Isodatetimeconverter convert = new Isodatetimeconverter ();
    2. Convert.  DateTimeFormat = "Yyyy-mm-dd HH:mm:ss.fff";
    3. string ret = Jsonconvert.serializeobject (bean, formatting.none, convert);


The Mybean type definition is used in this article:
Java code
  1. Import Java.sql.Timestamp;
  2. Public class Mybean {
  3. private String name;
  4. private String value;
  5. Private Timestamp TM;
  6. Public String GetName () {
  7. return name;
  8. }
  9. public void SetName (String name) {
  10. this.name = name;
  11. }
  12. Public String GetValue () {
  13. return value;
  14. }
  15. public void SetValue (String value) {
  16. this.value = value;
  17. }
  18. Public Timestamp Gettm () {
  19. return TM;
  20. }
  21. public void Settm (Timestamp tm) {
  22. This.tm = TM;
  23. }
  24. }
[Java]View Plaincopy
  1. Import Java.sql.Timestamp;
  2. Public class Mybean {
  3. private String name;
  4. private String value;
  5. Private Timestamp TM;
  6. Public String GetName () {
  7. return name;
  8. }
  9. public void SetName (String name) {
  10. this.name = name;
  11. }
  12. Public String GetValue () {
  13. return value;
  14. }
  15. public void SetValue (String value) {
  16. this.value = value;
  17. }
  18. Public Timestamp Gettm () {
  19. return TM;
  20. }
  21. public void Settm (Timestamp tm) {
  22. This.tm = TM;
  23. }
  24. }


Postscript:

I'm using older. NET 2.0, and the JSON conversion for C # is native-supported in a high-level version, and there's a better solution in the framework of 4.0/4.5, and we look forward to feedback.

In addition, C # when passing data to Java, its generated JSON often contains extra fields, the server-side parsing may error, the workaround is as follows:

The perfect solution for JSON date format transfer between Java and C #

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.