Como desserializar esse JSON em um objet
Estou tentando usar o JSON.Net para desserializar um objeto JSON em um objeto C
O objeto que eu quero criar éMonthlyPerformance
, que contém uma lista deType
, que contém uma lista deCategories
, que por sua vez contém uma lista deFunds
. Eles são definidos como:
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()
{
}
}
Eu pensei que o seguinte faria isso, mas não é. Está apenas criando uma instância doType
objeto com tudo nulo:
var file = File.ReadAllText(filePath);
var types = JsonConvert.DeserializeObject<Type>(file);
Este é o JSON que estou usando:
{
"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"
}
]
}
]
}
]
}
}
O que preciso fazer para que isso funcione?
editado para incluirMonthlyPerformance