使用Json让Java和C#沟通的方法
最近很忙啊,新项目下来了,都没时间写博客了。频率降低点,但不能不总结跟大家分享啊。
我们在项目里经常要涉及到各模块间的通信,这其中又不可避免要碰到各类语言间之间的通信,比如之前做的一个项目里面就是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
|
public class NotifyRealTimeMessage implements Serializable { 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 ; } } |
下面的是一个通用的做各类C#对象与Json字串之间转化的工具类,它使用泛型来实现,它提供了对象与Json串互转的两个方法。
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串。
这样,Java端和C#端都做好了,搞了一个新耳机,还没煲好,煲耳机去喽!
使用Json让Java和C#沟通的方法的更多相关文章
- JSONObject.fromObject(map)(JSON与JAVA数据的转换)
JSON与JAVA数据的转换(JSON 即 JavaScript Object Natation,它是一种轻量级的数据交换格式,非常适合于服务器与 JavaScript 的交互.) 上一篇文章中有这么 ...
- json学习系列(8)JSON与JAVA数据的相互转换实例
一.完整案例 先定义一个java实体对象,如下: package com.pcitc.json.cnblog; /** * SimInfo实体对象 * * @Description * @author ...
- JSON 与JAVA对象之间的转换(转)
JSON与XML两者目前在数据交互方面都有很高的使用率,随着现在SOA的兴起,异构系统的整合地位相应提高,本篇文章主要介绍JSON与JAVA对象之间的相互转换. 一.对普通类型的JSON模式的转换 一 ...
- net.sf.json.JSONException: java.lang.NoSuchMethodException
在尝试将json对象转换为list时候出现了如下错误 Exception in thread "main" net.sf.json.JSONException: java.lang ...
- JSON与JAVA数据的相互转换
http://www.cnblogs.com/linjiqin/archive/2011/09/19/2181408.html import net.sf.json.JSONArray; import ...
- Json字符串转换为java对象的各种实现方法【json_lib框架、Gson、org.json】
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://mengzhengbin520.blog.51cto.com/7590564/12 ...
- JSON和JAVA的POJO的相互转换【转载】
该类中用用到的jar包:参加文章<使用json-lib进行Java和JSON之间的转换[转载]> import java.util.Collection; import java.util ...
- json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
注:在项目中, 我使用原生的ajax请求数据的时候, JSONObject没能帮我解析, 当却不给我报错, 我是在junit单元测试中测试的时候, 发现的.发现好多时候, 特别是通过ajax请求, 不 ...
- 介绍4款json的java类库 及 其性能测试
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Programming Lan ...
随机推荐
- [LeetCode179]Largest Number
题目: Given a list of non negative integers, arrange them such that they form the largest number. For ...
- 构建安全的Xml Web Service系列之初探使用Soap头
原文:构建安全的Xml Web Service系列之初探使用Soap头 Xml Web Service 从诞生那天就说自己都么都么好,还津津乐道的说internet也会因此而进入一个新纪元,可5年多来 ...
- 经验19--C#大事
以前学过C#大事.但我还没有搞懂怎么弄. 这一次,他们下进行了研究. 1.定义参数类,对于参数的传递活动.(可以省略) public class UserEventArgs { ...
- ios 多线程开发(一)简介
简介 线程是在一个程序中并发的执行代码的方法之一.虽然有一些新的技术(operations, GCD)提供了更先进高效的并发实现,OS X和iOS同时也提供了创建和维护线程的接口. 这里将要介绍线程相 ...
- Android - 用Fragments实现动态UI
要在Android上实现动态的多窗口的用户交互界面,需要把UI组建和activity放到modules中, 这样才能划进划出activity.可是使用Fragment类来创建modules,它就像一个 ...
- POJ 3356 AGTC(最长公共子)
AGTC Description Let x and y be two strings over some finite alphabet A. We would like to transform ...
- Kotlin
关于Kotlin,网上已有一些介绍的文章,包括Antonio Leiva的这组blog翻译稿.不过,我还是想跟进它们.翻译它们,以锻炼自己的英文翻译.各位高手发现问题,请及时“拍砖”. 原文题目:Ko ...
- 单元测试React
React单元测试——十八般兵器齐上阵,环境构建篇 一个完整.优秀的项目往往离不开单元测试的环节,就 github 上的主流前端项目而言,基本都有相应的单元测试模块. 就 React 的项目来说,一套 ...
- Log4j配置文件位置+Spring数据源配置文件位置
一个.Log4j配置文件位置 1.加载自己主动 当应用程序启动,缺省情况下,这将是src文件夹搜索log4j.xml型材.如果不存在.我们将继续寻找log4j.properties文件,仅仅要找到当中 ...
- Go如何发送广播包
发送网络数据包成三种方式,每间单播.组播.广播. 广播通俗地讲,就是让你的机器发送的数据包能够被同一个网络内的全部主机都接收到. 在解说怎样发送广播包之前.先来看看跟广播有关的知识: 我们都知道IP地 ...