gpt4 book ai didi

c# - 如何将此 JSON 反序列化为对象?

转载 作者:太空狗 更新时间:2023-10-30 00:56:13 25 4
gpt4 key购买 nike

我正在尝试使用 JSON.Net 将 JSON 对象反序列化为 C# 对象。

我要创建的对象是 MonthlyPerformance,其中包含 Type 列表,其中包含 Categories 列表,后者又包含Funds 列表。它们被定义为:

public class MonthlyPerformance
{
public List<Type> Types { get; set; }
}

public class Type
{
public int Id { get; set; }
public string CountryId { get; set; }
public string Name { get; set; }

public List<Category> Categories { get; set; }

public Type()
{

}
}

public class Category
{
public int Id { get; set; }
public string CountryId { get; set; }
public string Name { get; set; }

public List<ConfigurationFund> Funds { get; set; }

public Category()
{

}
}

public class Fund
{
public int Id { get; set; }
public string CountryId { get; set; }
public string Name { get; set; }

public Fund()
{

}
}

我以为下面的会做,但事实并非如此。它只是创建一个 Type 对象的实例,所有内容都为空:

var file = File.ReadAllText(filePath);

var types = JsonConvert.DeserializeObject<Type>(file);

这是我正在使用的 JSON:

{
"MonthlyPerformance": {
"Type": [
{
"id": "65",
"countryId": "IE",
"name": "Irish Domestic Funds (Gross)",
"Category": [
{
"id": "25003334",
"countryId": "IE",
"name": "UK Equity",
"ConfigurationFund": [
{
"id": "25000301",
"countryId": "IE",
"name": "Aviva Irl UK Equity Fund"
},
{
"id": "25000349",
"countryId": "IE",
"name": "New Ireland UK Equity 9"
}
]
},
{
"id": "25003339",
"countryId": "IE",
"name": "Irish Equity",
"Fund": [
{
"id": "25000279",
"countryId": "IE",
"name": "Friends First Irish Equity G"
},
{
"id": "25000305",
"countryId": "IE",
"name": "Irish Life Celticscope 2 G"
}
]
}
]
},
{
"id": "80",
"countryId": "IE",
"name": "Irish Individual Pensions",
"Category": [
{
"id": "25003347",
"countryId": "IE",
"name": "Asia Pacific Ex-Japan Equity",
"Fund": [
{
"id": "25001789",
"countryId": "IE",
"name": "Aviva Irl Pacific Basin Eq"
},
{
"id": "25002260",
"countryId": "IE",
"name": "Ir Life Pacific Eq Indexed P"
}
]
},
{
"id": "25003365",
"countryId": "IE",
"name": "Flexible Equity",
"Fund": [
{
"id": "25003238",
"countryId": "IE",
"name": "Friends First Protected Equity Plus Fund S2"
},
{
"id": "25003267",
"countryId": "IE",
"name": "Friends First Protected Equity Plus Bond G"
}
]
}
]
}
]
}
}

我需要做什么才能让它发挥作用?

编辑以包含 MonthlyPerformance

最佳答案

我认为问题出在您的 Json 字符串中。

"Type": [ ... ] 

应该是

"Types": [ ... ]  

Types 是应该反序列化的属性的名称,您错误地Type 改为类名。

Type 类中的 Categories 属性也是如此。

另外 我只是简单地从 Json 字符串中删除了 "MonthlyPerformance" 根,它就像一个魅力。当然是使用 Json.NET。

这是修改后的 Json 的片段,具有适当的属性名称(通知、类型、类别、基金和缺少 MonthlyPerformance 根)

{
"Types": [
{
"id": "65",
"countryId": "IE",
"name": "Irish Domestic Funds (Gross)",
"Categories": [
{
"id": "25003334",
"countryId": "IE",
"name": "UK Equity",
"Funds": [
{
"id": "25000301",
"countryId": "IE",
"name": "Aviva Irl UK Equity Fund"
},
{
"id": "25000349",
"countryId": "IE",
"name": "New Ireland UK Equity 9"
}
]
}

关于c# - 如何将此 JSON 反序列化为对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8249454/

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