c# – ASP.NET WebAPI 2嵌套JSON
我已经坚持了一段时间,我似乎无法弄明白.感谢任何帮助!
这是我的模特:http://www.jsoneditoronline.org/?id=9ee3466c40627f33c284e63544c8b8a7 我有适当的C#对象设置如下: public class Media { public string name { get; set; } public string title { get; set; } public string album { get; set; } public string artist { get; set; } public string length { get; set; } public int bitrate { get; set; } public double size { get; set; } public string start_time { get; set; } public string mimetype { get; set; } public string hash { get; set; } } public class Playlist { public string name { get; set; } public List<Media> media { get; set; } public List<Graphics> graphics { get; set; } public bool shuffle { get; set; } public int volume { get; set; } public string start_time { get; set; } public string end_time { get; set; } } public class Day { public string name { get; set; } public List<Playlist> playlists { get; set; } } public class Schedule { public List<Day> days { get; set; } public string hash { get; set; } } 我需要直接从MVC控制器POST整个JSON对象.在其他情况下,我想把时间表.我该如何妥善处理?实例可能真的有帮助. 谢谢! 我已经在POST下面做了以下事情: var schedule = JsonConvert.DeserializeObject<Schedule>(model.ToString()); 这是按预期工作的,但是,有时相关的Media对象已经存在于数据库中,并且在尝试INSERT同一个Media对象(已经存在)时导致内部服务器错误 – Media的[Key]是hash属性. 解决方法
您需要序列化Day类.
使用Newtonsoft.json nuget包你需要将它序列化为对象.它会自动将复杂对象序列化为json List<Day> days = // list of days result var jsonData= JsonConvert.SerializeObject(days); return json(jsonData); 更新 根据您的更新序列化和反序列化功能正常工作.在Media中插入记录时遇到问题. > what is hash ? is it unique? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |