我們在項目裡經常要涉及到各模組間的通訊,這其中又不可避免要碰到各類語言間之間的通訊,比如之前做的一個項目裡面就是Java發的訊息需要C#接收,(具體訊息是怎麼傳輸的可以使用RabbitMQ等,關於RabbitMQ的使用總結可以看我之前的部落格),都是物件導向的語言,而物件導向的訊息怎麼反解析到C#是個難題。下面就是使用Json密器讓Java和C#溝通的具體辦法的總結。
摘要:Json是Java和C#之間通訊的利器,Java端將Java對象轉變為Json串後發出,C#端接收到Json串後轉換為C#對象;C#發出轉變為Json串的對象,Java收到後解析成Java對象,Json串在不同語言之間起到一個橋樑的作用。對定義的Java或C#對象產生Json字串,以及從Json字串產生Java或C#對象,有很方便的方法,那就是Java下使用jackson,C#下使用Newtonsoft.Json,其中還有一些問題需要注意,如關於時間這種常見類型轉換的問題,以下便是我對這方面的總結。
關鍵詞:Json,Java,C#,jackson,Newtonsoft
前提:Java寫的某種程式,C#寫的某種程式。
需求:Java程式和C#程式它們之間需要交換某些資訊,資訊原本是用對象的形式封裝的。
說明:使用jackson-all-1.9.0.jar及Newtonsoft.Json.dll。
一、Java
下面是一個簡單的Java類樣本,它包含了3個屬性,並且提供了對象與Json串互轉的兩個方法。
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; } /** * 產生Json串 * */ public String toJson() throws JsonGenerationException, JsonMappingException, IOException { return mapper.writeValueAsString(this); } /** * 從Json字串構建NotifyRealTimeMessage對象 * */ public static NotifyRealTimeMessage fromJson(String json) throws JsonParseException, JsonMappingException, IOException { if (json == null) { return null; } else { return mapper .readValue(json, NotifyRealTimeMessage.class); } } }
toJson方法將NotifyRealTimeMessage對象轉化為一個Json字串,fromJson靜態方法將一個Json串轉化為一個NotifyRealTimeMessage對象,由於NotifyRealTimeMessage對象中包含一個時間類型的Calendar欄位,故事先需要給mapper設定約定好的時間格式,mapper.SetDateFormat。這樣使用它:NotifyRealTimeMessage notifyMessage = NotifyRealTimeMessage.fromJson(json);String json=notifyMessage.toJson();。
查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/csharp/
二、C#
以下是與Java類對應的C#類,它也包含了三個屬性,但沒提供與Json串轉換的方法,注意JsonProperty標籤裡的名字跟Java類裡的一樣。
public class RealTimeDataMsg{ [JsonProperty("messageType")] public int MessageType { get; set; } [JsonProperty("geoData")] public GeoData Data { get; set; } [JsonProperty("time")] public DateTime Time { get; set; } }
下面的是一個通用的做各類C#對象與Json字串之間轉化的工具類,它使用泛型來實現,它提供了對象與Json串互轉的兩個方法。
public static class JsonHelper{ private static readonly JsonSerializerSettings MyJsonSerializerSettings; static JsonHelper() { MyJsonSerializerSettings = 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); }}
在C#中,使用起來也很方便,RealTimeDataMsg realMsg = JsonHelper.FromJson<RealTimeDataMsg>(json);string json = JsonHelper.ToJson(realMsg);。這裡同樣需要給MyJsonSerializerSettings設定好事先約定的時間格式:yyyy-MM-dd HH:mm:ss,這樣才能正確的解析Java產生的Json串。
這樣,Java端和C#端都做好了,搞了一個新耳機,還沒煲好,煲耳機去嘍!