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

本文关键字:JSON 自定义 响应 NET 使用 | 更新日期: 2023-09-27 18:05:16

这是我第一次尝试用json.net解析json响应,我完全迷路了。我在下面包含了解析json的一部分。我要做的是循环遍历backlinks数组。我试过从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
                            },

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

首先,如前所述,您提供的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名称只是数字,因此可能会出现问题,因此您可能也应该检查一下。