0

I get the error below... Something is very wrong :( any ideas? (This is in a Windows Phone 8 app)

An exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.DLL but was not handled in user code

And the code is

  string responseBody = @" {""HighScoreId"":1,""Name"":""Debra Garcia"",""Score"":2.23},{""HighScoreId"":2,""Name"":""Thorsten Weinrich"",""Score"":2.65}";

            GlobalHighScore s = JsonConvert.DeserializeObject<GlobalHighScore>(responseBody);

and the class is

  public class GlobalHighScore
{
    public int HighScoreId { get; set; }
    public string Name { get; set; }
    public double Score { get; set; }
}
4

1 回答 1

1

您的 JSON 有多个对象,并且都不在数组中。您要么需要从 JSON 中删除其中一个对象,要么将它们添加到数组中并正确反序列化它们:

string responseBody = 
    @"[
        {""HighScoreId"":1,""Name"":""Debra Garcia"",""Score"":2.23},
        {""HighScoreId"":2,""Name"":""Thorsten Weinrich"",""Score"":2.65}
    ]";

var highScores = 
    JsonConvert.DeserializeObject<List<GlobalHighScore>>(responseBody);
于 2013-06-13T21:37:23.263 回答