gpt4 book ai didi

c# - 使用 C# [Uncertain Object name] 解析 JSON

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

我将使用 C# 和 Newtonsoft.Json 将 JSON 数据解析为类。但是 JSON 数据与平常有点不同。这是 JSON 数据示例:

{
"ip_addresses": [
"192.168.1.1"
],
"ptr": {
"192.168.1.1": "www.example.com"
}
}

所以问题是由于ip_addresses 中的IP 而改变了ptr 中的IP。我已经成功地将 ip_addresses 数据解析到列表中。但我不知道下一步如何处理存储的 IP 地址列表。

class Server
{
public string hostname { get; set; }
public List<string> ip_addresses { get; set; }
public override string ToString()
{
string ip_set = string.Empty;
foreach (string ip in ip_addresses)
{
ip_set += string.Format("{0} ", ip);
}
return string.Format("{0} {1}\n", hostname, ip_set)
}
}
class Program
{
static void Main(string[] args)
{
//json data is from the web response
string responseContent = reader.ReadToEnd();
Server server = JsonConvert.DeserializeObject<Server>(responseContent);

Console.WriteLine(server);
Console.ReadKey();
}
}

非常感谢:D

最佳答案

猜测ptr不是数组。
那么下面的属性可能会起作用

public Dictionary<string,string> ptr {get; set;} 

如果是数组,则设为List<Dictionary<string,string>>

我在手机上,还没有检查过。请验证

更新:忘记添加
使用 ptr["ipAddress"] 访问它

关于c# - 使用 C# [Uncertain Object name] 解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34835590/

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