Introduction
Project data format If yes Json format, we recommend using LitJson parsing and Newtonsoft.json
Details and Download Library
VS comes Nuget recommended to use
Newtonsoft.Json: https://www.newtonsoft.com/json
LitJson: https://litjson.net/
Serialization and de-serialization
Serialization: converting the object state can be kept to a process format or transmitted.
Deserialization: the stream into an object.
Popular talk, the sequence of data for transmission, user data is stored deserialized.
Use Json
Open Visual Studio, select Tools IDE above options - "Nuget Package Manager -" Nuget management solution package
Json example
Simple analytical
Initial data
{
"total": 1,
"code": 0,
"rows": [
{
"id": 1013,
"name": "QB",
"version": "1.0.2",
"size": 707608
}
]
}
LitJson source
using LitJson2;
String str = "{\"total\":1,\"code\":0,\"rows\":[{\"id\":1013,\"name\":\"QB\",\"version\":\"1.0.2\",\"size\":707608}]}";
public static void LitJson(string DataJson)
{
JsonData json = JsonMapper.ToObject(DataJson);
if((int)json["code"]==0) //判断一级目录下code是否为0
{
JsonData data = json["rows"][0];
int id = (int)data["id"];
string name = data["name"].ToString();
Console.WriteLine(id+name);
}
Console.ReadLine();
}
Newtonsoft.json simple analytical
using Newtonsoft.Json.Linq;
String str = "{\"total\":1,\"code\":0,\"rows\":[{\"id\":1013,\"name\":\"QB\",\"version\":\"1.0.2\",\"size\":707608}]}";
public static void Newtonsoft(string DataJson)
{
JObject json = JObject.Parse(DataJson);
if ((int)json["code"] == 0) //判断一级目录下code是否为0
{
string dataS = json["rows"][0].ToString();
JObject data = JObject.Parse(dataS);
int id = (int)data["id"];
string name = data["name"].ToString();
Console.WriteLine(id + name);
}
Console.ReadLine();
}
operation result
Json serialization and deserialization
/// <summary>
/// 学生信息实体
/// </summary>
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public Class Class { get; set; }
}
/// <summary>
/// 学生班级实体
/// </summary>
public class Class
{
public int ID { get; set; }
public string Name { get; set; }
}
public static void Newtonsoft(string DataJson)
{
Student stu = new Student();
stu.ID = 1; //自己定义Json数据
stu.Name = "张三";
stu.Class = new Class() { ID = 0121, Name = "CS0121" };
//使用方法1
//实体序列化、反序列化
//结果:{"ID":1,"Name":"张三","Class":{"ID":121,"Name":"CS0121"}}
string json1 = JsonConvert.SerializeObject(stu);
Console.WriteLine(json1);
Student stu2 = JsonConvert.DeserializeObject<Student>(json1);
Console.WriteLine(stu2.Name + "---" + stu2.Class.Name);
Console.ReadLine();
}
Download Project
https://gitee.com/PErobin/DllTest.git
Reference blog
Introduction and use C # Json tool --Newtonsoft.Json sequence of https://blog.csdn.net/u011127019/article/details/51706619
Newtonsoft.Json json string parsing process (the most clear and understandable way) HTTPS: / /blog.csdn.net/u010388954/article/details/79741069