加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – ASP.NET WebAPI 2嵌套JSON

发布时间:2020-12-15 17:13:52 所属栏目:百科 来源:网络整理
导读:我已经坚持了一段时间,我似乎无法弄明白.感谢任何帮助! 这是我的模特:http://www.jsoneditoronline.org/?id=9ee3466c40627f33c284e63544c8b8a7 我有适当的C#对象设置如下: public class Media{ public string name { get; set; } public string title { g
我已经坚持了一段时间,我似乎无法弄明白.感谢任何帮助!

这是我的模特: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?
> Can 2 different string have the same hash code in C#
> Socks,birthdays and hash collisions
> Preventing Duplicates: Hash Table vs. Dictionary vs. Binary Search Tree

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读