使用JSON.NET反序列化json - 无法反序列化,因为类型需要JSON数组

时间:2014-12-11 13:48:05

标签: c# json json.net

尝试通过API读取json,但收到以下错误:我尝试了一些但似乎总是出现此错误..

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type     'System.Collections.Generic.List`1[Shared.Review]' because the type requires a JSON array     (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'status', line 1, position 10.

型号:

public class Reviewer
{
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string verified_buyer { get; set; }
    public string profile_picture { get; set; }
}

public class Review
{
    public string product_review_id { get; set; }
    public string name { get; set; }
    public string review { get; set; }
    public string rating { get; set; }
    public string date_created { get; set; }
    public string timeago { get; set; }
    public string date_formatted { get; set; }
    public string product { get; set; }
    public List<object> ratings { get; set; }
    public Reviewer reviewer { get; set; }
}

public class RootObject
{
    public string status { get; set; }
    public List<Review> reviews { get; set; }
    public int count { get; set; }
    public string rating { get; set; }
    public string per_page { get; set; }
    public string current_page { get; set; }
    public int total_pages { get; set; }
}

c#代码:

  var reviews = JsonConvert.DeserializeObject<List<Review>>(json);
  StringBuilder reviewsString = new StringBuilder();
  foreach (var review in reviews)
  {
       reviewsString.AppendFormat("<div class=\"review\">");
       reviewsString.AppendFormat("<p class=\"review-title\">Snugg Case</p>");
       reviewsString.AppendFormat("<div class=\"rating\">");
       reviewsString.AppendFormat("<span class=\"star\"></span>");
       reviewsString.AppendFormat("<span class=\"star\"></span>");
       reviewsString.AppendFormat("<span class=\"star\"></span>");
       reviewsString.AppendFormat("<span class=\"star\"></span>");
       reviewsString.AppendFormat("<span class=\"halfStar\"></span>");
       reviewsString.AppendFormat("</div>");
       reviewsString.AppendFormat("<p class=\"review-details\">{0}</p>",
                                                         review.review);
       reviewsString.AppendFormat("<p class=\"review-name\">{0}</p>",
                                                           review.name);
       reviewsString.AppendFormat("<p class=\"review-date\">{0}</p>",
                                                 review.date_formatted);
       reviewsString.AppendFormat("</div>  ");
       topSectionReviews.Text += reviewsString;
   }

示例Json:

http://pastebin.com/HNhNDMhr

任何问题只是问

提前致谢,

迈克尔

1 个答案:

答案 0 :(得分:2)

您正在尝试反序列化为集合:

var reviews = JsonConvert.DeserializeObject<List<Review>>(json);

然而,你的Json不是顶级的集合,因为这意味着json字符串将以[]开头和结尾。相反,它被{}包围,表示单个对象。

您希望将其反序列化为单个RootObject

var reviews = JsonConvert.DeserializeObject<RootObject>(json);

因为它有一个字段List<Review> reviewsstring status

并非您没有遵循命名约定。使用正确的命名和[JsonProperty("something")]属性来正确解析json。