Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

c# - Automapper:Converting JSON to list of objects

Source Object (JSON, using JSON.NET if it matters):

{
    "conum" : 1001,
    "name" : "CLN Industries Corporation",
    "agencyName" : "Murphy, Holmes & Associates, LLC",
    "sAA" : [{
            "code" : 247,
            "description" : "Mechanicu0027s lien - Bond to Discharge - Fixed penalty - where principal has posted Performance and Pa"
        }, {
            "code" : 277,
            "description" : "Mechanicu0027s lien - Bond to Discharge - Open Penalty - where principal has posted Performance and Paym"
        }, {
            "code" : 505,
            "description" : "Indemnity Bonds - Contractoru0027s Indemnity Against Damages where there is a performance bond and addit"
        }
    ]
}

Destination Object (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 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"]); });
    }
}

Everything works except for the SaaCodes conversion, where Automapper converts each entry into an empty SaaCode object (all properties set to null).

How/where do I tell automapper how to convert items in that JSON field into its destination type?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this out -

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"" : ""Mechanicu0027s lien - Bond to Discharge - Fixed penalty - where principal has posted Performance and Pa""
                                    }, {
                                        ""code"" : 277,
                                        ""description"" : ""Mechanicu0027s lien - Bond to Discharge - Open Penalty - where principal has posted Performance and Paym""
                                    }, {
                                        ""code"" : 505,
                                        ""description"" : ""Indemnity Bonds - Contractoru0027s Indemnity Against Damages where there is a performance bond and addit""
                                    }
                                ]
                            }
                        ";

        var jsonoObj = JObject.Parse(jsonText);

        CorporateRatesInfo dto = mapper.Map<CorporateRatesInfo>(jsonoObj);



    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...