当前位置:网站首页>Summary of the use of C # Jason code in TCP communication
Summary of the use of C # Jason code in TCP communication
2022-07-27 06:08:00 【The moonlight is shining】
Json Encoding in TCP Summary of some uses in communication
Json Use
1、 Through management Nunet Package , download Newtonsoft.Json.DLL Assembly 
2、 Import package in program ;
using Newtonsoft.Json;
3、 Create classes that need to be serialized , Design the attribute structure in the class , encapsulation , structure
4、 serialize
It is written as follows :
string r= JsonConvert.SerializeObject(new Class name ( Fill in the corresponding attribute value according to the structure ));
The serialized string structure is as follows :
Deserialization is written as follows :
string UserJson = "{\"UNO\":\"1\"," +
" \"UName\":\" dragon \"," +
" \"Uage\":\"21\"," +
" \"Uaddress\":\" China \"," +
" \"Uphone\":\"151 3692 3546\"}";
User Data = JsonConvert.DeserializeObject<User>(UserJson);
Json How to use it
When used in specific projects , As receiver , Most of the data I receive is based on a format document , Write and send to me , There was no problem with it . But carefully observe the format of sending instructions in the format document , You will find some of the same fields , For example, instruction code , Machine code , Equipment address and other fields . But there are different . It can only be said that the field names with the same meaning are the same .
therefore , Think about serialization and deserialization , What happens if the properties of their corresponding objects are different .
After a series of tests , The following results are obtained :
1、 When the attribute of the object serialized by the sender is greater than and contains the object attribute corresponding to the deserialization of the receiver , The deserialized object will only extract the value of the same field and return .
2、 When the attributes of the object serialized by the sender are smaller than those of the object deserialized by the receiver , The deserialized object will only initialize the value of the corresponding field according to the corresponding field and return .
Conclusion , The receiver doesn't have to set the same class as the sender to deserialize , Just set a class that wants to get the field as needed .
Because in the project , I need to receive different instructions with the same field , So I put all possible attributes in all instructions into one class as attributes , In this way, different instructions can be deserialized with a class .( I'm really a little smart ) In this way, there is no need to set different classes to deserialize instructions according to the corresponding instructions , Reduce the complexity of the project .
Specific code link
Baidu cloud disk :TCP.rar.
bsqy
Introduce : The code is a test TCP A small example of communication , The data between communications goes through Json Send after serializing the encoding , The receiver parses the contents of the instruction through deserialization .
Tools :VS2017
边栏推荐
猜你喜欢

Super remote connection management tool: Royal TSX

When multiple formulas in latex share a sequence number

geonode geoserver win10 安装教程(亲测)

Lightroom Classic 2022 v11.4中文版「最新资源」

力扣每日一题(链表模拟)

切线空间以及TBN矩阵

STM32-红外遥控

Unity 引擎开始从 Mono 迁移到 .NET CoreCLR

Weidongshan digital photo frame project learning (IV) simple TXT document display (e-paper book)

力扣每日一题 剑指 Offer II 091. 粉刷房子
随机推荐
Weidongshan digital photo frame project learning (I) display ASCII characters on LCD
韦东山 数码相框 项目学习(二)在LCD上显示中文字符
pycharm安装及导入项目注意事项
编程学习记录——第8课【数组与设计五子棋,扫雷游戏】
WebODM win10安装教程(亲测)
人月神话阅读笔记
[first song] rebirth of me in py introductory training (1)
[first song] rebirth of me in py introductory training (3): if conditional sentence
[first song] rebirth of me in py introductory training (2): formula programming
[first song] rebirth of me in py introductory training (4): Circular program
C#线程锁(Lock)
非重叠矩形中的随机点(力扣每日一题)
[first song] machine learning of rebirth - linear regression
发布 分辨率0.22m的建筑物分割数据库
Weidongshan digital photo frame project learning (IV) simple TXT document display (e-paper book)
Lightroom classic 2022 v11.4 Chinese version "latest resources"
[first song] deep learning of rebirth -keras (elementary)
arcgis for js api(2) 获取要素服务的id集合
一张图看懂指针
力扣160. 相交链表