为了账号安全,请及时绑定邮箱和手机立即绑定

关于List类型的json字符串反序列化的问题

关于List类型的json字符串反序列化的问题

C#
喵喵时光机 2018-12-06 12:30:56
Person person = JsonHelper.JsonToEntity<Person>(str); var customfields = JsonHelper.JsonToList<CustomFields>(str); List<Images> image = JsonHelper.JsonToList<Images>(str); c.Code = c.Code; string sql = "insert into dbo.A(UserID,BatchID,CityCode,CustomFields,Images,Time,LineName,Note,PO_Code,Pile,Version,Lat,Lon,Alt,Type,CFCode)values('" + person.UserID + "','" + person.BatchID + "','" + person.CityCode + "','" + person.CustomFields + "','" + person.Images + "','" + person.Time + "','" + person.LineName + "','" + person.Note + "','" + person.PO_Code + "','" + person.Pile + "'," + person.Version + "," + person.Lat + "," + person.Lon + "," + person.Alt + "," + person.Type + ",'" + customfields. + "')"; SqlConnection conn = new SqlConnection(connStr); SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); string path2 = "E:/国省道采集数据"; path = Environment.CurrentDirectory; string pattern = "Command.txt"; string[] strFileName = Directory.GetFiles(path2, pattern); foreach (var item in strFileName) { File.Delete(item); } //var xuliehua = str; //StreamReader sr = new StreamReader(@"E:\国省道采集数据\Command.txt", Encoding.GetEncoding("gb2312")); //MessageBox.Show(sr.ToString()); } toolStripProgressBar1.Value = 0; MessageBox.Show("解压缩成功!"); } else { MessageBox.Show("警告:请选择要进行批量解压缩的文件!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch { } } private void txtfiles2_TextChanged(object sender, EventArgs e) { } [DataContract] public class Person { public string UserID { get; set; } public string BatchID { get; set; } public string CityCode { get; set; } public List<CustomFields> CustomFields { get; set; } public List<Images> Images { get; set; } public string Time { get; set; } public string LineName { get; set; } public string Note { get; set; } public string PO_Code { get; set; } public string Pile { get; set; } public int Version { get; set; } public double Lat { get; set; } public double Lon { get; set; } public double Alt { get; set; } public int Type { get; set; } } [Serializable] public class CustomFields { [DataMember] public string Code { get; set; } [DataMember] public string Value { get; set; } } [Serializable] public class Images { public string Time { get; set; } public string FileName { get; set; } public double Lon { get; set; } public double Lat { get; set; } public double Alt { get; set; } } public class JsonHelper { public static List<T> JsonToList<T>(string jsonStr) { JavaScriptSerializer js = new JavaScriptSerializer(); //List<T> t = (List<T>) js.Deserialize(str, typeof(List<T>)); List<T> t = js.Deserialize<List<T>>(jsonStr); return t; } public static T JsonToEntity<T>(string jsonStr) { JavaScriptSerializer js = new JavaScriptSerializer(); T t = js.Deserialize<T>(jsonStr);//(T)js.Deserialize(str, typeof(T)); return t; } } 字符串为{"UserID":"huangshan1@78","BatchID":"1489281487936","CityCode":"341000","CustomFields":[{"Code":"GLDWMC","Value":"徽州交通局"},{"Code":"QLMC","Value":"杨村2号桥"},{"Code":"QLBH","Value":"K3001"},{"Code":"JCSJ","Value":""},{"Code":"QLCD","Value":"134"},{"Code":"SJHZ","Value":"1"},{"Code":"AKJF","Value":"2"},{"Code":"AJCHNXF","Value":"1"},{"Code":"B_ISDANGER","Value":"2"},{"Code":"KJZC","Value":"125"},{"Code":"DKZDKJ","Value":"25"},{"Code":"QMQK","Value":"8.5"},{"Code":"QMJK","Value":"7.4"},{"Code":"LXBM","Value":"S103341000"},{"Code":"LDXLH","Value":"001"}],"Images":[{"Time":"Mar 12, 2017 12:00:00 AM","FileName":"/storage/emulated/0/easyroad/collectionpo/1489282287821.jpeg","Lon":118.1775853,"Lat":29.97436336,"Alt":0.0}],"Time":"2017-03-12","LineName":"S103341000","Note":"","PO_Code":"T_ql","Pile":"5.092","Version":2,"Lat":29.97436336,"Lon":118.1775853,"Alt":0.0,"Type":4} 现在person的可以正常转化,但是CustomFields这个就无法反序列化然后 customfields也点不出来东西
查看完整描述

6 回答

?
三国纷争

TA贡献1804条经验 获得超7个赞

现在person的可以正常转化,但是CustomFields这个就无法反序列化然后

customfields也点不出来东西

 ---》》小伙,这里不是这么用的,要想取customfields中的数据,你得遍历啊!因为它是一个集合。。。Ienumerable怎么能直接点出来。

反序列化前首先你得弄清楚json数据是对象还是数组,然后再定义对应的实体,调对应的反序列化方法。json的两种数据类型,可以去了解下。

 
查看完整回答
反对 回复 2018-12-24
?
交互式爱情

TA贡献1712条经验 获得超3个赞

你的json没问题,类没问题,那应该是反序列化出了问题。

试试我的:

            Person person = new Person();
            person = JsonConvert.DeserializeObject<Person>(json);

你的json中包含了三个类,所以它们是一个整体,要一起发反序列化。然后再从person对象中去取出里面的对象。

查看完整回答
反对 回复 2018-12-24
?
白板的微信

TA贡献1883条经验 获得超3个赞

小伙,这个json里不是包含了三个类,你可以看下上面定义的person类的结构。

查看完整回答
反对 回复 2018-12-24
?
隔江千里

TA贡献1906条经验 获得超10个赞

@~扎克伯格: 一个Person,一个CustomFields,一个Images,json字符串里没有同时出现这三个类的信息?

查看完整回答
反对 回复 2018-12-24
?
慕森王

TA贡献1777条经验 获得超3个赞

@龙行天涯: 没有啊!我只看到了一段object格式的json中包含了UserID、BatchID等key name以及包含多个object的两个数组(CustomFields、Images)。

查看完整回答
反对 回复 2018-12-24
?
慕的地10843

TA贡献1785条经验 获得超8个赞

@~扎克伯格: 你赢了

查看完整回答
反对 回复 2018-12-24
  • 6 回答
  • 0 关注
  • 669 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信