• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#解析复杂的Json成Dictionarykey,value并保存到数据库(多方法解析Json四) ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

准备工作:

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

2、.net3.5+版本都有,如果VS2010找不到,在这个文件夹找:C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.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);

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c++获取GMT时间和字符串发布时间:2022-07-13
下一篇:
【C#】将数据库读出的数据转换为DataTable类型集合发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap