C# 如何将此JSON反序列化为对象?
我试图使用JSON.Net将JSON对象反序列化为C#对象 我要创建的对象是C# 如何将此JSON反序列化为对象?,c#,json,json.net,deserialization,C#,Json,Json.net,Deserialization,我试图使用JSON.Net将JSON对象反序列化为C#对象 我要创建的对象是MonthlyPerformance,它包含类型的列表,它包含类别的列表,而类别又包含基金的列表。它们的定义如下: public class MonthlyPerformance { public List<Type> Types { get; set; } } public class Type { public int Id { get; set; } public string
MonthlyPerformance
,它包含类型的列表,它包含类别的列表,而类别又包含基金的列表。它们的定义如下:
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()
{
}
}
这是我正在使用的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"
}
]
}
]
}
]
}
}
我需要做些什么才能让它工作
编辑为包含<代码>月度性能<代码>您认为使用<代码>动态代码>对象而不是反序列化到对象吗?(未声明月度业绩
,类型
,类别
,基金
)
用法:
dynamic jobj = JsonUtils.JsonObject.GetDynamicJsonObject(JsonString);
foreach (var item in jobj.MonthlyPerformance.Type)
{
Console.WriteLine(item.name);
foreach (var category in item.Category)
{
Console.WriteLine("\t" + category.name);
if (category.ConfigurationFund != null)
{
foreach (var fund in category.ConfigurationFund)
{
Console.WriteLine("\t\t" + fund.name);
}
}
}
}
所需的Helper类是您的类的上述代码乍一看是正确的
我已经成功地用以下类(JavaScriptSerializer)反序列化了JSON。用法如下
using System.Web.Script.Serialization;
JavaScriptSerializer js = new JavaScriptSerializer();
string file = File.ReadAllText(filePath);
MonthyPerformance json = js.Deserialize<MonthlyPerformance>(file);
我相信问题出在Json字符串中
"Type": [ ... ]
应该是
"Types": [ ... ]
Types
是应该反序列化的属性的名称,您错误地将Type
改为类名
类型
类中的类别
属性也是如此
另外我只是从Json字符串中删除了“MonthlyPerformance”
根,它工作起来很有魅力。当然是Json.NET
下面是修改后的Json片段,其中包含适当的属性名称(注意,类型,类别,资金以及缺少MonthlyPerformance根)
您的属性名与JSON的属性名不匹配。我预计序列化会失败
考虑到你发布的JSON,我希望你的c#类是这样的。我不熟悉JSON.net,但我假设他们有一个属性装饰器,允许您指定c#prop也匹配的JSON属性的名称
public class MonthlyPerformance
{
public List<Type> Type { get; set; }
}
public class Type
{
public int id { get; set; }
public string countryId { get; set; }
public string Name { get; set; }
public List<Category> Category { get; set; }
public Type()
{
}
}
public class Category
{
public int id { get; set; }
public string countryId { get; set; }
public string name { get; set; }
public List<Fund> ConfigurationFund{ get; set; }
public Category()
{
}
}
public class Fund
{
public int id { get; set; }
public string countryId { get; set; }
public string name { get; set; }
public Fund()
{
}
}
public class MonthlyPerformance
{
公共列表类型{get;set;}
}
公共类类型
{
公共int id{get;set;}
公共字符串countryId{get;set;}
公共字符串名称{get;set;}
公共列表类别{get;set;}
公共类型()
{
}
}
公共类类别
{
公共int id{get;set;}
公共字符串countryId{get;set;}
公共字符串名称{get;set;}
公共列表配置基金{get;set;}
公共类别()
{
}
}
公营基金
{
公共int id{get;set;}
公共字符串countryId{get;set;}
公共字符串名称{get;set;}
公帑()
{
}
}
这就是我用于JSON反序列化的内容
using System.Web.Script.Serialization;
namespace blahblah
{
public partial class AccessTierService : ServiceBase
{
public static T ia_deserialize_json<T>(string json_string)
{
try
{
if ((String.Compare(json_string, null) == 0) ||
(String.Compare(json_string, "") == 0) ||
(String.Compare(json_string, String.Empty) == 0))
{
return default(T);
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
return (T)serializer.Deserialize<T>(json_string);
}
catch (Exception)
{
return default(T);
}
}
}
}
使用System.Web.Script.Serialization;
名称空间blahblah
{
公共部分类AccessTierService:ServiceBase
{
公共静态TIA_反序列化_json(字符串json_字符串)
{
尝试
{
if((String.Compare(json_String,null)==0)||
(String.Compare(json_String,“”==0)||
(String.Compare(json_String,String.Empty)==0))
{
返回默认值(T);
}
JavaScriptSerializer serializer=新的JavaScriptSerializer();
返回(T)序列化程序。反序列化(json_字符串);
}
捕获(例外)
{
返回默认值(T);
}
}
}
}
叫它
login_request = ia_deserialize_json<ol_login_request>(body_string);
login\u request=ia\u反序列化\u json(body\u字符串);
您认为您的代码是指您定义的类型还是.net中预定义的类型。再看看Hi@DarthVader,我可以看到type
的类型在我自己的名称空间中。即使我将其更改为尝试创建MonthlyPerformance
的实例(我已用此更新了问题),它的行为仍然相同。
public class MonthlyPerformance
{
public List<Type> Type { get; set; }
}
public class Type
{
public int id { get; set; }
public string countryId { get; set; }
public string Name { get; set; }
public List<Category> Category { get; set; }
public Type()
{
}
}
public class Category
{
public int id { get; set; }
public string countryId { get; set; }
public string name { get; set; }
public List<Fund> ConfigurationFund{ get; set; }
public Category()
{
}
}
public class Fund
{
public int id { get; set; }
public string countryId { get; set; }
public string name { get; set; }
public Fund()
{
}
}
using System.Web.Script.Serialization;
namespace blahblah
{
public partial class AccessTierService : ServiceBase
{
public static T ia_deserialize_json<T>(string json_string)
{
try
{
if ((String.Compare(json_string, null) == 0) ||
(String.Compare(json_string, "") == 0) ||
(String.Compare(json_string, String.Empty) == 0))
{
return default(T);
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
return (T)serializer.Deserialize<T>(json_string);
}
catch (Exception)
{
return default(T);
}
}
}
}
login_request = ia_deserialize_json<ol_login_request>(body_string);