使用JSON.NET解析自定义JSON响应

时间:2015-07-29 12:47:24

标签: c# json json.net

这是我尝试用json.net解析json响应的第一次尝试,我完全迷失了。我在下面包含了解析的json的一部分。我想做的是循环反向链接数组。我试过从newtonsoft文档中实现各种样本,但它们似乎不起作用,我认为这是因为我的json不匹配他们的样本而且我不知道如何做必要的更正。如果有人能提供一些C#代码让我入门,我会非常感激。

谢谢, 混沌

{
"accounts": [
    {
        "10555": {
            "sites": [
                {
                    "12222": {
                        "pages_indexed_in_bing": {},
                        "download_time": null,
                        "backlinks": [
                            {
                                "anchor_text": "websites for insurance agents",
                                "source_url": "http://win-winbusinesses.com/insurance/how-to-building-an-effective-insurance-website/",
                                "found_on": "2015-07-15",
                                "page_authority": null,
                                "link_strength": 3,
                                "domain": "win-winbusinesses.com",
                                "domain_authority": 17
                            },

1 个答案:

答案 0 :(得分:0)

首先,如前所述,您提供的JSON无效。 我认为它应该是这样的:

{
"accounts": [
    {
        "10555": {
            "sites": [
                {
                    "12222": {
                        "pages_indexed_in_bing": {

                        },
                        "download_time": null,
                        "backlinks": [
                            {
                                "anchor_text": "websites for insurance agents",
                                "source_url": "http://win-winbusinesses.com/insurance/how-to-building-an-effective-insurance-website/",
                                "found_on": "2015-07-15",
                                "page_authority": null,
                                "link_strength": 3,
                                "domain": "win-winbusinesses.com",
                                "domain_authority": 17
                            }
                        ]

                }
            }
        ]
    }
  }
 ]
}

根据这个JSON,如果你想让newtonsoft成功解析,你的类应该是这样的:

public class PagesIndexedInBing
{
}

public class Backlink
{
    public string anchor_text { get; set; }
    public string source_url { get; set; }
    public string found_on { get; set; }
    public object page_authority { get; set; }
    public int link_strength { get; set; }
    public string domain { get; set; }
    public int domain_authority { get; set; }
}

public class __invalid_type__12222
{
    public PagesIndexedInBing pages_indexed_in_bing { get; set; }
    public object download_time { get; set; }
    public List<Backlink> backlinks { get; set; }
}

public class Site
{
    public __invalid_type__12222 __invalid_name__12222 { get; set; }
}

public class __invalid_type__10555
{
    public List<Site> sites { get; set; }
}

public class Account
{
    public __invalid_type__10555 __invalid_name__10555 { get; set; }
}

public class RootObject
{
    public List<Account> accounts { get; set; }
}

正如您所看到的那样,由于您使用的数字/ var名称可能存在问题,因此您可能也应该检查它。