如何将此JSON反序列化为对象? [英] How do deserialize this JSON into an object?
问题描述
我正在尝试使用JSON.Net将JSON对象反序列化为C#对象.
I'm trying to use JSON.Net to deserialize a JSON object into a C# object.
我要创建的对象是MonthlyPerformance
,其中包含Type
的列表,其中包含Categories
的列表,而该列表又包含Funds
的列表.它们定义为:
The object I want to create is MonthlyPerformance
which contains a list of Type
, which contains a list of Categories
, which in turn contains a list of Funds
. They are defined as:
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
对象的实例,并且所有内容均为空:
I thought the following would do it, but it's not. It's just creating an instance of the Type
object with everything null:
var file = File.ReadAllText(filePath);
var types = JsonConvert.DeserializeObject<Type>(file);
这是我正在使用的JSON:
This is the JSON that I'm using:
{
"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"
}
]
}
]
}
]
}
}
要使它正常工作,我需要做什么?
What do I need to do to get this to work?
已修改为包含MonthlyPerformance
推荐答案
我相信问题出在您的Json字符串中.
I believe the problem is in your Json string.
"Type": [ ... ]
应该是
"Types": [ ... ]
Types
是应反序列化的属性的名称,您错误地放置Type
类名.
Types
is the name of property that should be deserialized, you mistakenly put Type
the class name instead.
Type
类中的Categories
属性也是如此.
The same goes for Categories
property in the Type
class.
还,我只是从Json字符串中删除了"MonthlyPerformance"
根,它的工作原理就像一个魅力.当然有了Json.NET.
Also I simply removed "MonthlyPerformance"
root from the Json string and it worked like a charm. With Json.NET of course.
以下是经过修改的Json的代码片段,具有适当的属性名称(注意,类型,类别,资金和缺少MonthlyPerformance根)
Here is a snippets of the modified Json with appropriate property names (notice, Types, Categories, Funds and the absence of MonthlyPerformance root)
{
"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"
}
]
}
这篇关于如何将此JSON反序列化为对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!