2

这是代码:

            var jss = new JavaScriptSerializer();
            GResponse dict = jss.Deserialize<GResponse>(jsonText);

这些是类:

public class GResponse
{
    public List<GResponseData> responseData { get; set; }
    public string responseDetails { get; set; }
    public int responseStatus { get; set; }

}

public class GResponseData
{
    public List<GResult> results { get; set; }
    public List<GCursor> cursor { get; set; }
}

public class GResult
{
    public string GsearchResultClass { get; set; }
    public string unescapedUrl { get; set; }
    public string url { get; set; }
    public string visibleUrl { get; set; }
    public string cacheUrl { get; set; }
    public string title { get; set; }
    public string titleNoFormatting { get; set; }
    public string content { get; set; }
}

public class GCursor
{
    public string resultCount { get; set; }
    public List<GPages> PagesItems { get; set; }
    public string estimatedResultCount { get; set; }
    public int currentPageIndex { get; set; }
    public string moreResultsUrl { get; set; }
    public string searchResultTime { get; set; }
}
public class GPages
{
    public string start { get; set; }
    public int label { get; set; }
}

很长一段时间以来,我一直在尝试反序列化以下 json 字符串但没有成功。

{
    "responseData": {
        "results": [
            {
                "GsearchResultClass": "GwebSearch",
                "unescapedUrl": "irrelevant",
                "url": "irrelevant",
                "visibleUrl": "irrelevant",
                "cacheUrl": "irrelevant",
                "title": "irrelevant",
                "titleNoFormatting": "irrelevant",
                "content": "irrelevant"
            },
            {
                "GsearchResultClass": "GwebSearch",
                "unescapedUrl": "irrelevant",
                "url": "irrelevant",
                "visibleUrl": "irrelevant",
                "cacheUrl": "irrelevant",
                "title": "irrelevant",
                "titleNoFormatting": "irrelevant",
                "content": "irrelevant"
            }
        ],
        "cursor": {
            "resultCount": "irrelevant",
            "pages": [
                {
                    "start": "0",
                    "label": 1
                },
                {
                    "start": "28",
                    "label": 8
                }
            ],
            "estimatedResultCount": "irrelevant",
            "currentPageIndex": 0,
            "moreResultsUrl": "irrelevant",
            "searchResultTime": "0.27"
        }
    },
    "responseDetails": null,
    "responseStatus": 200
}

似乎没有出现任何错误,但是当我检查 dict.responseData.Count 时,由于某种原因它总是等于 0。提前致谢!

4

2 回答 2

1

复制你的 Json。转到 Visual Studio,然后编辑 > 选择性粘贴 -> 将 JSON 粘贴为类。您现在将看到您的 Json 反序列化为 c# 的样子,然后可以相应地调整您现有的 c# 类。

注意:您必须打开一个 C# 文件才能在剪贴板(已复制)中获取“选择性粘贴”选项以及一些有效的 JSON。

于 2013-10-08T14:40:41.497 回答
0

如果resonseData是列表,您还应该在 JSON 中将其作为列表/数组,因此请更改:

"responseData": {
   "results": ...
   "cursor: ...
}

到数组,其中包含一个元素:

"responseData": [
    {
        "results": ...
        "cursor: ...
    }
]
于 2013-03-07T19:07:39.030 回答