JSON.NET读取字符串时出错。意外的令牌:StartObject。路径' responseData',

时间:2016-08-03 17:05:09

标签: json json.net

我正在尝试反序列化此link,但我一直收到此错误。

读取字符串时出错。意外的令牌:StartObject。路径' responseData'。

从我用谷歌搜索,问题似乎是我试图反序列化的对象的设置。以下是我的课程:

  public class FeedSearchResult
{
    [JsonProperty("responseData")]
    public String ResponseData { get; set; }

    [JsonProperty("query")]
    public String Query { get; set; }

    [JsonProperty("entries")]
    public string[] Entries { get; set; }

    [JsonProperty("responseDetails")]
    public object ResponseDetails { get; set; }

    [JsonProperty("responseStatus")]
    public String ResponseStatsu { get; set; }
}

public class ResultItem
{
    [JsonProperty("title")]
    public String Title { get; set; }

    [JsonProperty("url")]
    public String Url { get; set; }

    [JsonProperty("link")]
    public String Link { get; set; }
}

我班上做错了什么?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您的数据模型只有两个嵌套级别,但返回的JSON有三个级别。如果您使用https://jsonformatter.curiousconcept.com/查看格式化的JSON,您会看到:

{
   "responseData":{
      "query":"Official Google Blogs",
      "entries":[
         {
            "url":"https://googleblog.blogspot.com/feeds/posts/default",
            "title":"\u003cb\u003eOfficial Google Blog\u003c/b\u003e",
            "contentSnippet":"\u003cb\u003eOfficial\u003c/b\u003e weblog, with news of new products, events and glimpses of life inside \u003cbr\u003e\n\u003cb\u003eGoogle\u003c/b\u003e.",
            "link":"https://googleblog.blogspot.com/"
         },

特别是当数据模型需要是一个包含的对象时,它的responseDataString。这是异常的具体原因。

如果您将JSON上传到http://json2csharp.com/,您将获得以下数据模型,可用于反序列化此JSON:

public class ResultItem
{
    public string url { get; set; }
    public string title { get; set; }
    public string contentSnippet { get; set; }
    public string link { get; set; }
}

public class ResponseData
{
    public string query { get; set; }
    public List<ResultItem> entries { get; set; }
}

public class RootObject
{
    public ResponseData responseData { get; set; }
    //Omitted since type is unclear.
    //public object responseDetails { get; set; } 
    public int responseStatus { get; set; }
}
相关问题