• C#解析复杂的Json成Dictionary<key,value>并保存到数据库(多方法解析Json 四)


    准备工作:

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

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

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

    4、using Newtonsoft.Json,下载Newtonsoft.Json

    解析Json,一般方法:反序列化(次要)

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

     通过下标循环Dictionary的键值集合(次要)

    
    
    for (int i = 0; i < jarr.Count; i++)
    {
    var item = jarr.ElementAt(i);//获取字典的下标为i的<key,value>值
    var itemKey = item.Key;  //获取上面得到的key值
    var itemValue = item.Value;//获取上面得到的value值

    }
     

    实例:(主要)

     KeyValuePair来遍历Dictionary

            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"}]}";
                Dictionary<string, object> jarr = js.Deserialize<Dictionary<string, object>>(json);
                string OfflineLock_id = "";
                foreach (KeyValuePair<string, object> j in jarr)
                {
    
                    //string OfflineLock_id = "";
                    string ZwjyzsList_id = "";
                    string ZwjyzsList_sfyfz = "";
                    string ZwjyzsList_zwjyzsbh = "";
    
                    if (j.Key.Equals("offlineLock"))
                    {
                        string json2 = JsonConvert.SerializeObject(j.Value);
                        List<OfflineLock> list = JsonConvert.DeserializeObject<List<OfflineLock>>(json2);
                        foreach (OfflineLock ol in list)
                        {
                            Console.WriteLine(ol.id);
                            OfflineLock_id = ol.id;
                            Console.WriteLine(ol.mac);
                        }
                    }
                    if (j.Key.Equals("resultList"))
                    {
                        string pid = OfflineLock_id;
                        string json2 = JsonConvert.SerializeObject(j.Value);
                        List<ZwjyzsList> list = JsonConvert.DeserializeObject<List<ZwjyzsList>>(json2);
                        foreach (ZwjyzsList zl in list)
                        {
                            //Console.WriteLine(zl.zwjyzsbh);
                            ZwjyzsList_id = zl.id;
                            ZwjyzsList_sfyfz = zl.sfyfz;
                            ZwjyzsList_zwjyzsbh = zl.zwjyzsbh;
                            string sql = @" insert into jy_offline_lock_zslb(id,pid,zwjyzsbh,sfyfz) 
                                values(@id,@pid,@zwjyzsbh,@sfyfz) ";
    
                            DBDao.ExecuteSql(sql, new SQLiteParameter("@id", ZwjyzsList_id), new SQLiteParameter("@pid", pid)
                                , new SQLiteParameter("@zwjyzsbh", ZwjyzsList_zwjyzsbh), new SQLiteParameter("@sfyfz", ZwjyzsList_sfyfz));
                   
                        }
                        
                    }
    
    
                    
                }
                Console.ReadLine();

     实体类:

    1、整个Json看成4个<key,value>,offlineLock 这个key的value有多条记录,用List保存

    class Lock
        {
            public List<OfflineLock> offlineLock { get; set; }
            public string flag { get; set; }
            public string status { get; set; }
            public List<ZwjyzsList> resultList { get; set; }
    
        }

    2、

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

    3、

    class ZwjyzsList
        {
            public string id { get; set; }
            public string sfyfz { get; set; }
            public string zwjyzsbh { get; set; }
        }

     4、DBDao.cs

    public static int ExecuteSql(string sql, params SQLiteParameter[] parameters)
            {
    
                using (SQLiteConnection con = new SQLiteConnection(DATA_SOURCE))
                {
                    con.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand())
                    {
                        cmd.Connection = con;
                        cmd.CommandText = sql;
                        cmd.Parameters.AddRange(parameters);
                        return cmd.ExecuteNonQuery();
    
                    }
                }
    
            }

    解析:

    (1)当用下面Dictionary反序列化解析这个Json数据时,保存为4个<key,value>

    Dictionary<string, object> jarr = js.Deserialize<Dictionary<string, object>>(json);

    (2)其中offlineLock 这个大<key,value>对的value又是几个小<key,value>对,

    如果要取到小<key,value>的值,需要对offlineLock 这条记录重新<序列化-再反序列化>

    string json2 = JsonConvert.SerializeObject(j.Value);
    List<OfflineLock> list = JsonConvert.DeserializeObject<List<OfflineLock>>(json2);
  • 相关阅读:
    log输出到日志和控制台
    CRM--搜索功能
    CRM--对数据进行排序
    CRM-注意的小事情
    CRM--modelform之instance
    CRM--保留原搜索条件
    crm系统
    Django多个app情况下静态文件的配置
    测试
    题库
  • 原文地址:https://www.cnblogs.com/Donnnnnn/p/6020353.html
Copyright © 2020-2023  润新知