0

Trying to read json via an API but getting the following error: I've tried a few things but always seem to get this error..

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.

Model:

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# Code:

  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;
   }

Example Json:

http://pastebin.com/HNhNDMhr

Any questions just ask

Thanks in advance,

Michael

1 Answer 1

2

You're trying to deserialize into a collection:

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

However your Json isn't a collection at the top level since that would mean the json string would start and end with []. Instead it is surrounded by {} which indicates a single object.

It looks like you want to deserialize as a single RootObject instead:

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

since this has a field List<Review> reviews and string status.

Not that you are not following naming conventions. Use proper naming and the [JsonProperty("something")] attribute to correctly parse the json.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.