Json C#解析

介绍

项目中数据格式如果是是Json格式,推荐大家使用LitJson和Newtonsoft.json进行解析

库的详细介绍和下载地址

推荐使用VS自带的Nuget来使用
Newtonsoft.Json:https://www.newtonsoft.com/json
LitJson:https://litjson.net/

序列化和反序列化

序列化:将对象状态转换成可保持或传输的格式的过程。
反序列化:将流转换成对象。
通俗讲,序列化用于数据传输,反序列用户数据存储。

Json使用

打开Visual Studio,选择IDE上面的工具选项--》Nuget包管理器--》管理解决方案的Nuget程序包

Json示例

简单解析

初始数据

{
    "total": 1,
    "code": 0,
    "rows": [
        {
            "id": 1013,
            "name": "QB",
            "version": "1.0.2",
            "size": 707608
        }
    ]
}

LitJson源码

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简单解析

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();
        }

运行结果

Json序列化和反序列化

/// <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();
        }

项目下载地址

https://gitee.com/PErobin/DllTest.git

参考博客

C# Json序列化工具--Newtonsoft.Json的简介和使用https://blog.csdn.net/u011127019/article/details/51706619
Newtonsoft.Json解析json字符串处理(最清晰易懂的方法)https://blog.csdn.net/u010388954/article/details/79741069

猜你喜欢

转载自www.cnblogs.com/aqyl/p/11281274.html