在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
准备工作: 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++)
实例:(主要) 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);
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论