当前位置:网站首页>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
边栏推荐
- 力扣题解 动态规划(1)
- PS 2022 updated in June, what new functions have been added
- C语言-文件操作
- 2022.6.10 STM32MP157串口时钟的学习
- 力扣题解 二叉树(7)
- Unity 桌面7.6 版本解读
- Baiwen driver Daquan learning (I) LCD driver
- Cesium教程 (1) 界面介绍-3dtiles加载-更改鼠标操作设置
- Weidongshan digital photo frame project learning (I) display ASCII characters on LCD
- 一张图看懂指针
猜你喜欢
随机推荐
2022.6.10 stm32mp157 serial port clock learning
切线空间以及TBN矩阵
制作视频特效必备工具:NUKE 13
发布 分辨率0.22m的建筑物分割数据库
Dpdk network protocol stack VPP OVS DDoS Sdn nfv virtualization high performance expert Road
AE 3D particle system plug-in: Trapcode particle
力扣题解 动态规划(1)
编程学习记录--第2课【初识C语言】
Unity 窗口界面的简单介绍
常见的SQL优化方法
Redis在windows下的idea连接不上问题
非真实感渲染(NPR)论文理解及其复现(Unity) - 《Stylized Highlights for Cartoon Rendering and Animation》
[song] rebirth of me in py introductory training (8): module
论文报告-Linear Regression for face recognition
浅记一下十大排序
[first song] Introduction to data science of rebirth -- return to advanced level
力扣题解 动态规划(5)
1 semi automatic crawler
[headline] Rebirth: the basis of CNN image classification
Gbase 8C - SQL reference 6 SQL syntax (14)









