自动映射器:将JSON转换为对象列表 [英] Automapper:Converting JSON to list of objects
本文介绍了自动映射器:将JSON转换为对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
源对象(JSON,如果需要的话,使用JSON.NET):
Source Object (JSON, using JSON.NET if it matters):
{
"conum" : 1001,
"name" : "CLN Industries Corporation",
"agencyName" : "Murphy, Holmes & Associates, LLC",
"sAA" : [{
"code" : 247,
"description" : "Mechanic\u0027s lien - Bond to Discharge - Fixed penalty - where principal has posted Performance and Pa"
}, {
"code" : 277,
"description" : "Mechanic\u0027s lien - Bond to Discharge - Open Penalty - where principal has posted Performance and Paym"
}, {
"code" : 505,
"description" : "Indemnity Bonds - Contractor\u0027s Indemnity Against Damages where there is a performance bond and addit"
}
]
}
目标对象(C#):
public class CorporateRatesInfo
{
public string Conum { get; set; }
public string Name { get; set; }
public string AgencyName { get; set; }
public List<SaaCode> SaaCodes { get; set; }
}
public class SaaCode
{
public string Code { get; set; }
public string Description { get; set; }
}
Automapper配置和映射:
Automapper config and mappings:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<JObject, CorporateRatesInfo>();
cfg.AddProfile<CorporateRatesProfile>();
});
//config.AssertConfigurationIsValid();
_mapper = config.CreateMapper();
public class CorporateRatesProfile : Profile
{
protected override void Configure()
{
CreateMap<JObject, CorporateRatesInfo>()
.ForMember("SaaCodes", cfg => { cfg.MapFrom(jo => jo["sAA"]); })
.ForMember("Conum", cfg => { cfg.MapFrom(jo => jo["conum"]); })
.ForMember("Name", cfg => { cfg.MapFrom(jo => jo["name"]); })
.ForMember("AgencyName", cfg => { cfg.MapFrom(jo => jo["agencyName"]); });
}
}
除SaaCodes转换(自动映射器将每个条目转换为空的SaaCode对象(所有属性均设置为null))外,所有其他操作均有效.
Everything works except for the SaaCodes conversion, where Automapper converts each entry into an empty SaaCode object (all properties set to null).
如何/在何处告诉automapper如何将JSON字段中的项目转换为目标类型?
How/where do I tell automapper how to convert items in that JSON field into its destination type?
推荐答案
尝试一下-
public class CorporateRatesInfo
{
public string Conum { get; set; }
public string Name { get; set; }
public string AgencyName { get; set; }
public List<SaaCode> SaaCodes { get; set; }
}
public class SaaCode
{
public string Code { get; set; }
public string Description { get; set; }
}
public class CorporateRatesProfile : Profile
{
protected override void Configure()
{
CreateMap<JObject, SaaCode>()
.ForMember("Code", cfg => { cfg.MapFrom(jo => jo["code"]); })
.ForMember("Description", cfg => { cfg.MapFrom(jo => jo["description"]); });
CreateMap<JObject, CorporateRatesInfo>()
.ForMember("SaaCodes", cfg => { cfg.MapFrom(jo => jo["sAA"]); })
.ForMember("Conum", cfg => { cfg.MapFrom(jo => jo["conum"]); })
.ForMember("Name", cfg => { cfg.MapFrom(jo => jo["name"]); })
.ForMember("AgencyName", cfg => { cfg.MapFrom(jo => jo["agencyName"]); });
}
}
class Program
{
static void Main(string[] args)
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<JObject, CorporateRatesInfo>();
cfg.AddProfile<CorporateRatesProfile>();
});
//config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
var jsonText = @"
{
""conum"" : 1001,
""name"" : ""CLN Industries Corporation"",
""agencyName"" : ""Murphy, Holmes & Associates, LLC"",
""sAA"" : [{
""code"" : 247,
""description"" : ""Mechanic\u0027s lien - Bond to Discharge - Fixed penalty - where principal has posted Performance and Pa""
}, {
""code"" : 277,
""description"" : ""Mechanic\u0027s lien - Bond to Discharge - Open Penalty - where principal has posted Performance and Paym""
}, {
""code"" : 505,
""description"" : ""Indemnity Bonds - Contractor\u0027s Indemnity Against Damages where there is a performance bond and addit""
}
]
}
";
var jsonoObj = JObject.Parse(jsonText);
CorporateRatesInfo dto = mapper.Map<CorporateRatesInfo>(jsonoObj);
}
}
这篇关于自动映射器:将JSON转换为对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文