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



尝试通过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

任何问题都只是问

提前感谢,

迈克尔

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

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

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

看起来您想反序列化为单个RootObject

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

因为这有一个领域List<Review> reviewsstring status.

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

相关内容

  • 没有找到相关文章