C# - 将JSON解析为多个对象

时间:2018-02-21 13:09:59

标签: c# json json.net

我正在尝试从JSON结果中创建多个对象。 我的JSON看起来像这样:

修改

[{"quality":"hd","type":"video/mp4","width":1920,"height":1080,"link":"http://player.vimeo.com/external/255898412.hd.mp4?s=8766561d230749d75a0ddde2ddfbeb69e0e5198e&profile_id=175&oauth2_token_id=1040381751","created_time":"2018-02-15T15:46:25+02:00","fps":23.98,"size":3113207678,"md5":"b6beed65b699df870e481045178accc5","link_secure":"https://player.vimeo.com/external/255898412.hd.mp4?s=8766561d230749d75a0ddde2ddfbeb69e0e5198e&profile_id=175&oauth2_token_id=1040381751"},{"quality":"sd","type":"video/mp4","width":640,"height":360,"link":"http://player.vimeo.com/external/255898412.sd.mp4?s=b51b55f6bd6e1af2a8f48571e5804d91e6a82533&profile_id=164&oauth2_token_id=1040381751","created_time":"2018-02-15T15:46:05+02:00","fps":23.98,"size":536864946,"md5":"af227a5526af15d2bce6ac951d6cf06b","link_secure":"https://player.vimeo.com/external/255898412.sd.mp4?s=b51b55f6bd6e1af2a8f48571e5804d91e6a82533&profile_id=164&oauth2_token_id=1040381751"},{"quality":"sd","type":"video/mp4","width":960,"height":540,"link":"http://player.vimeo.com/external/255898412.sd.mp4?s=b51b55f6bd6e1af2a8f48571e5804d91e6a82533&profile_id=165&oauth2_token_id=1040381751","created_time":"2018-02-15T15:46:05+02:00","fps":23.98,"size":1242328160,"md5":"1963f908509b14fd7a40dc46bfa6c519","link_secure":"https://player.vimeo.com/external/255898412.sd.mp4?s=b51b55f6bd6e1af2a8f48571e5804d91e6a82533&profile_id=165&oauth2_token_id=1040381751"},{"quality":"hd","type":"video/mp4","width":1280,"height":720,"link":"http://player.vimeo.com/external/255898412.hd.mp4?s=8766561d230749d75a0ddde2ddfbeb69e0e5198e&profile_id=174&oauth2_token_id=1040381751","created_time":"2018-02-15T15:46:05+02:00","fps":23.98,"size":1977386604,"md5":"af38f067bd39f4f5bb71bad72f925337","link_secure":"https://player.vimeo.com/external/255898412.hd.mp4?s=8766561d230749d75a0ddde2ddfbeb69e0e5198e&profile_id=174&oauth2_token_id=1040381751"},{"quality":"hls","type":"video/mp4","link":"https://player.vimeo.com/external/255898412.m3u8?s=f25b7114977a0c6b37739886da189051ed31999e&oauth2_token_id=1040381751","created_time":"2018-02-15T15:46:25+02:00","fps":23.98,"size":3113207678,"md5":"b6beed65b699df870e481045178accc5","link_secure":"https://player.vimeo.com/external/255898412.m3u8?s=f25b7114977a0c6b37739886da189051ed31999e&oauth2_token_id=1040381751"}]

应将其解析为3个对象。 我有一个VideoFileModel.cs类看起来像这样:

public partial class VideoFileModel
{
    [JsonProperty("quality")]
    public string Quality { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("width")]
    public long? Width { get; set; }

    [JsonProperty("height")]
    public long? Height { get; set; }

    [JsonProperty("link")]
    public string Link { get; set; }

    [JsonProperty("created_time")]
    public System.DateTime CreatedTime { get; set; }

    [JsonProperty("fps")]
    public double Fps { get; set; }

    [JsonProperty("size")]
    public long Size { get; set; }

    [JsonProperty("md5")]
    public string Md5 { get; set; }

    [JsonProperty("link_secure")]
    public string LinkSecure { get; set; }
}

我目前正在尝试做的是解析它:

string json = Helpers.HTTPFetch(url, method, headers, body, contentType);
var result = JsonConvert.DeserializeObject<VideoFileModel>(json);

但我很确定它不起作用,或者至少不是我希望它工作的方式。 我应该改变什么?

1 个答案:

答案 0 :(得分:3)

您应该在反序列化时使用List<>

var result = JsonConvert.DeserializeObject<List<VideoFileModel>>(json);

您的JSON中有一个VideoFileModel列表,您尝试将其反序列化为单个对象。

以下反序列化:

enter image description here

<强>更新

根据OP更新的JSON。它仍然被反序列化:

enter image description here