zoukankan      html  css  js  c++  java
  • 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();
  • 相关阅读:
    1349:【例4-10】最优布线问题
    1348:【例4-9】城市公交网建设问题
    P2024 [NOI2001]食物链
    $P2573 [SCOI2012]滑雪$
    $P1991 无线通讯网$
    $P2872 [USACO07DEC]道路建设Building Roads$
    $P1547 Out of Hay$
    hdu 3468 Treasure Hunting
    hungary HK 多重匹配
    Hdu匹配题集
  • 原文地址:https://www.cnblogs.com/Donnnnnn/p/6019729.html
Copyright © 2011-2022 走看看