How to Use Json to communicate with Java and C #

Source: Internet
Author: User
Tags tojson rabbitmq

Communication between modules is often involved in projects, which inevitably involves communication between different languages, for example, in the previous project, the Java message needs to be received by C # (how to transmit the specific message can use RabbitMQ, etc, for more information about the use of RabbitMQ, see my previous blog. These are all object-oriented languages. It is difficult to resolve object-oriented messages to C. The following is a summary of the specific methods for Java and C # to communicate with each other using the Json password. Abstract: Json is a powerful tool for communication between Java and C #. the Java end converts a Java object to a Json string and then sends it. The C # End receives the Json string and converts it to a C # object; C # parses the Json string into a Java object after receiving the string. The Json string serves as a bridge between different languages. It is convenient to generate a Json string for the defined Java or C # object and generate a Java or C # object from the Json string. That is, jackson is used in Java, C # Use Newtonsoft. json, there are still some issues to be aware of, such as time-based conversion of common types, the following is my summary of this. Key words: Json, Java, C #, jackson, Newtonsoft prerequisites: a program written by Java, a program written by C. Requirement: Some information needs to be exchanged between Java programs and C # programs. The information is originally encapsulated in the form of objects. (Use jackson-all-1.9.0.jar and Newtonsoft. Json. dll. 1. Java below is a simple Java class example. It contains three attributes and provides two methods for mutual conversion between objects and Json strings. PublicclassNotifyRealTimeMessage implementsSerializable {private static ObjectMapper mapper = new ObjectMapper (); static {SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); mapper. setDateFormat (dateFormat) ;}@ JsonProperty ("messageType") private int type; @ JsonProperty ("geoData") private Object message; @ JsonProperty ("time") private Calendar time; public int getType (){ Return type;} public void setType (int type) {this. type = type;} public Object getMessage () {return message;} public void setMessage (Object message) {this. message = message;} public Calendar getTime () {return time;} public void setTime (Calendar time) {this. time = time;}/*** produce Json String **/public String toJson () throws JsonGenerationException, JsonMappingException, IOException {return ma Pper. writeValueAsString (this);}/*** construct the policyrealtimemessage object from the Json String **/public static policyrealtimemessage fromJson (String json) throws JsonParseException, JsonMappingException, IOException {if (json = null) {return null;} else {return mapper. readValue (json, policyrealtimemessage. class) ;}} the toJson method converts the policyrealtimemessage object to a Json string. The fromJson static method converts a Json string into a policyrealtimemessage object. Because the NotifyRealTimeMessage object contains a time-type Calendar field, you must first set the agreed time format for mapper, mapper. SetDateFormat. Use it like this: yyrealtimemessage policymessage = yyrealtimemessage. fromJson (json); String json = policymessage. toJson ();. II. C # The following is the C # class corresponding to the Java class. It also contains three attributes, but does not provide a method for converting Json strings, note that the name of the JsonProperty tag is the same as that of the Java class. Public class RealTimeDataMsg {[JsonProperty ("messageType")] public int MessageType {get; set;} [JsonProperty ("geoData")] public GeoData Data {get; set ;} [JsonProperty ("time")] public DateTime Time {get; set ;}} is a common tool class for converting C # objects and Json strings, it is implemented using generics. It provides two methods for mutual conversion between objects and Json strings. Public static class JsonHelper {private static readonly JsonSerializerSettings MyJsonSerializerSettings; static JsonHelper () {response = new JsonSerializerSettings (); IsoDateTimeConverter dateTimeConverter = new IsoDateTimeConverter (); dateTimeConverter. dateTimeFormat = "yyyy-MM-dd HH: mm: ss"; MyJsonSerializerSettings. converters. add (dateTimeConverter);} public static T FromJson <T> (String json) {if (string. isNullOrEmpty (json) {return default (T);} return JsonConvert. deserializeObject <T> (json, MyJsonSerializerSettings);} public static string ToJson <T> (T data) {return JsonConvert. serializeObject (data, MyJsonSerializerSettings) ;}} in C #, it is also very convenient to use, RealTimeDataMsg realMsg = JsonHelper. fromJson <RealTimeDataMsg> (json); string json = JsonHelper. toJson (realMsg );. Here, you also need to set the agreed time format for MyJsonSerializerSettings: yyyy-MM-dd HH: mm: ss to parse the Json string generated by Java correctly.

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.