我们在项目里经常要涉及到各模块间的通信,这其中又不可避免要碰到各类语言间之间的通信,比如之前做的一个项目里面就是Java发的消息需要C#接收,(具体消息是怎么传输的可以使用RabbitMQ等,关于RabbitMQ的使用总结可以看我之前的博客),都是面向对象的语言,而面向对象的消息怎么反解析到C#是个难题。下面就是使用Json密器让Java和C#沟通的具体办法的总结。
一、Java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
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();。
二、C#
|
1
2
3
4
5
6
7
8
9
10
11
12
|
public class RealTimeDataMsg
{ [JsonProperty("messageType")]
public int MessageType { get; set; }
[JsonProperty("geoData")]
public GeoData Data { get; set; }
[JsonProperty("time")]
public DateTime Time { get; set; }
} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
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串。
转自:http://www.cnblogs.com/wgp13x/p/0effafd9f5283cbf36e62b4fb5e94c81.html