gpt4 book ai didi

c# - Automapper:将 JSON 转换为对象列表

转载 作者:太空狗 更新时间:2023-10-29 21:32:38 24 4
gpt4 key购买 nike

源对象(JSON,如果重要则使用 JSON.NET):

{
"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 配置和映射:

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 转换外,一切正常,Automapper 将每个条目转换为一个空的 SaaCode 对象(所有属性都设置为 null)。

我如何/在哪里告诉自动映射器如何将该 JSON 字段中的项目转换为其目标类型?

最佳答案

试试这个 -

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);



}
}

编辑:根据 https://docs.automapper.org/en/stable/Configuration.html , Automapper 版本 6 及以上不支持 Configure() 方法。所以我稍微调整了我的答案以遵循构造函数方法。

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
{
public CorporateRatesProfile()
{
CreateMap<JToken, SaaCode>()
.ForMember("Code", cfg => { cfg.MapFrom(jo => jo["code"]); })
.ForMember("Description", cfg => { cfg.MapFrom(jo => jo["description"]); });

CreateMap<JToken, 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.AddProfile<CorporateRatesProfile>();
});

var mapper = config.CreateMapper();
var jsonText = @"{""conum"":1001,""name"":""CLN Industries Corporation"",""agencyName"":""Murphy, Holmes & Associates, LLC"",""sAA"":[{""code"":247,""description"":""Mechanic's lien - Bond to Discharge - Fixed penalty - where principal has posted Performance and Pa""},{""code"":277,""description"":""Mechanic's lien - Bond to Discharge - Open Penalty - where principal has posted Performance and Paym""},{""code"":505,""description"":""Indemnity Bonds - Contractor's Indemnity Against Damages where there is a performance bond and addit""}]}";
var jsonoObj = JObject.Parse(jsonText);
CorporateRatesInfo dto = mapper.Map<CorporateRatesInfo>(jsonoObj);
}
}

关于c# - Automapper:将 JSON 转换为对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38107415/

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