• C# JavaScriptSerializer 解析Json数据(多方法解析Json 三)


    准备工作:

    1、添加引用System.Web.Extensions,

    2、.net3.5+版本都有,如果VS2010找不到,在这个文件夹找:C:Program FilesReference AssembliesMicrosoftFrameworkv3.5

    3、再using System.Web.Script.Serialization;即可

    实现:

    方法一

           var js = new System.Web.Script.Serialization.JavaScriptSerializer();
                string json = "{"offlineLock":[{"id":"4028d808581dab0f01581db51405001e","mac":"D4:3D:7E:5F:B7:44","sdsl":5,"sdrq":1477967156304,"shlb":"0"}],"flag":"success","status":"1400","resultList":[{"id":"4028d808581dab0f01581db5145c001f","zwjyzsbh":"1000001600000052","sfyfz":"0"},{"id":"4028d808581dab0f01581db514780020","zwjyzsbh":"1000001600000054","sfyfz":"0"},{"id":"4028d808581dab0f01581db514950021","zwjyzsbh":"1000001600000056","sfyfz":"0"},{"id":"4028d808581dab0f01581db514b20022","zwjyzsbh":"1000001600000058","sfyfz":"0"},{"id":"4028d808581dab0f01581db514cc0023","zwjyzsbh":"1000001600000060","sfyfz":"0"}]}";
                var jarr = js.Deserialize<Dictionary<string, object>>(json);
                foreach(var j in jarr)
                {
                    Console.WriteLine(string.Format("{0}:{1}", j.Key, j.Value));
                }
                Console.ReadLine();

    方法二:

    1、建好实体类,对应json数据里的key

    class Lock
        {
            public List<OfflineLock> offlineLock { get; set; }
            public string flag { get; set; }
            public string status { get; set; }
            public List<ResultList> resultList { get; set; } }
    class OfflineLock
        {
            public string id { get; set; }
            public string mac { get; set; }
            public long sdsl { get; set; }
            public long sdrq { get; set; }
            public string shlb { get; set; }
        }
    class ResultList
    { 

    public string id { get; set; }

    public string sfyfz { get; set; }

    public string zwjyzsbh { get; set; }
    }

    2、JavaScriptSerializer 解析Json数据

    string json = "{"offlineLock":[{"id":"4028d808581dab0f01581db51405001e","mac":"D4:3D:7E:5F:B7:44","sdsl":5,"sdrq":1477967156304,"shlb":"0"}],"flag":"success","status":"1400","resultList":[{"id":"4028d808581dab0f01581db5145c001f","zwjyzsbh":"1000001600000052","sfyfz":"0"},{"id":"4028d808581dab0f01581db514780020","zwjyzsbh":"1000001600000054","sfyfz":"0"},{"id":"4028d808581dab0f01581db514950021","zwjyzsbh":"1000001600000056","sfyfz":"0"},{"id":"4028d808581dab0f01581db514b20022","zwjyzsbh":"1000001600000058","sfyfz":"0"},{"id":"4028d808581dab0f01581db514cc0023","zwjyzsbh":"1000001600000060","sfyfz":"0"}]}";
    
                JavaScriptSerializer  js = new JavaScriptSerializer();
                Lock str=js.Deserialize<Lock>(json);
    
                Console.WriteLine(str.offlineLock[0].id);//控制台输入试试
                Console.ReadLine();
  • 相关阅读:
    adodb.stream文件操作类详解
    Html中Label标记的作用和使用介绍
    正则表达式的威力轻松消除HTML代码
    只需一行代码就能让IE 6崩溃
    码农干货系列【17】Wind.js与Promise.js
    码农干货系列【3】割绳子(cut the rope)制作点滴:旋转(rotation)
    HTML5 Canvas开发者和读者的福音
    码农干货系列【8】世界上最简单的3D渲染(no webgl)
    码农干货系列【18】getting started with Promise.js(总)
    ProgressForm
  • 原文地址:https://www.cnblogs.com/Donnnnnn/p/6019729.html
Copyright © 2020-2023  润新知