gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:36:35 24 4
gpt4 key购买 nike

这是我第一次尝试使用 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
},

最佳答案

首先,如前所述,您提供的 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; }
}

如您所见,由于您使用的类/变量名称仅为数字,因此可能会出现问题,因此您可能也应该检查一下。

关于c# - 使用 JSON.NET 解析自定义 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31700833/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com