C#Json.net解析值时遇到意外的字符:[

时间:2018-02-12 23:17:39

标签: c# .net json

public class JSON_Browse_Root
{
    public string browse_content { get; set; }
    public List<JSON_Browse_Content> Content { get; set; }
}

public class JSON_Browse_Content
{
    public string link { get; set; }
    public string image { get; set; }
    public string title { get; set; }
    public string description { get; set; }
    public string rating { get; set; }
    public string genre { get; set; }
}

using (var client = new HttpClient())
{
    using (HttpResponseMessage response = await client.GetAsync("https://myjsonurl.com"))
    using (HttpContent responseData = response.Content)
    {
        var jsonData = await responseData.ReadAsStringAsync();
        var root = JsonConvert.DeserializeObject<JSON_Browse_Root>(jsonData);
    }

}

JSON:

{
    "browse_content": [
        {
            "link": "https:\/\/blah",
            "image": "https:\/\/blahblah.blah.gif",
            "title": "Mr. Southern Hospitality",
            "description": "He got old money",
            "rating": "e",
            "author": "",
            "genre": "Comedy - Original"
        },
        {
            "link": "https:\/\/blah",
            "image": "https:\/\/blahblah.blah.png",
            "title": "Throwverwatch",
            "description": "Based on the competitive overwatch experience",
            "rating": "t",
            "author": "",
            "genre": "Comedy - Parody"
        }
  

发生了'Newtonsoft.Json.JsonReaderException'类型的异常   System.Private.CoreLib.ni.dll但未在用户代码中处理   解析值时遇到意外的字符:[

我在iOS和Android应用程序中使用了这个JSON,它运行得很好。此外,jsonlint.com说JSON是有效的。不确定为什么它在[character。

上错误

1 个答案:

答案 0 :(得分:2)

假设您的JSON由于复制/粘贴错误而缺少结尾,那么您遇到的实际问题是您的模型类是错误的。您有一个名为string的{​​{1}}属性,并且反序列化器正在尝试将JSON的数组(即browse_content)部分提供给它,因此您会收到有关意外字符的错误{ {1}}。因此,简单的解决方法是将模型更新为:

[...]