使用JSON.NET反序列化JSON的非常简单的尝试不起作用

时间:2013-06-13 21:35:08

标签: json json.net deserialization

我收到以下错误...有些问题:(任何想法?(这是在Windows Phone 8应用程序中)

Newtonsoft.Json.DLL中出现“Newtonsoft.Json.JsonReaderException”类型的异常,但未在用户代码中处理

代码是

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

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

,班级是

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

1 个答案:

答案 0 :(得分: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);