我正在尝试反序列化这个链接,但我一直收到这个错误。
读取字符串时出错。意外的标记: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; }
}
我在课堂上做错了什么?如有任何帮助,我们将不胜感激。
您的数据模型只有两个嵌套级别,但返回的JSON有三个。如果您使用https://jsonformatter.curiousconcept.com/你会看到:
{
"responseData":{
"query":"Official Google Blogs",
"entries":[
{
"url":"https://googleblog.blogspot.com/feeds/posts/default",
"title":"u003cbu003eOfficial Google Blogu003c/bu003e",
"contentSnippet":"u003cbu003eOfficialu003c/bu003e weblog, with news of new products, events and glimpses of life inside u003cbru003enu003cbu003eGoogleu003c/bu003e.",
"link":"https://googleblog.blogspot.com/"
},
特别是,当数据模型需要成为包含对象时,它将responseData
作为String
。这就是异常的具体原因。
如果您将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; }
}