我正在尝试在Xamarin表单应用程序中使用Event Espresso 4(EE4)REST API。我从EE4 REST API接收(作为JSON)的DTO非常复杂,我想将其平铺为将在应用程序中使用的“相当简单”的模型。
我从EE4 REST API收到的DTO如下:
[
{
"ATT_ID": 10686,
"ATT_full_name": "Four Nights",
"ATT_fname": "Four",
"ATT_lname": "Nights",
"ATT_email": "four.nights@yopmail.com",
"ATT_phone": "+18001579534565",
"registrations": [
{
"REG_ID": 183,
"EVT_ID": 10584,
"ATT_ID": 10686,
"TXN_ID": 66,
"TKT_ID": 4,
"STS_ID": "RAP",
"REG_date": "2019-02-13T09:30:00",
"REG_code": "66-4-1-24bf",
"REG_count": 1,
"REG_group_size": 1,
"REG_date_gmt": "2019-02-13T08:30:00",
"_calculated_fields": {
"_protected": []
},
"transaction": {
"TXN_ID": 66,
"STS_ID": "TCM",
"payments": [
{
"PAY_ID": 20,
"TXN_ID": 66,
"STS_ID": "PAP",
"PMD_ID": 11
}
]
},
"answers": [
{
"ANS_ID": 112,
"ANS_value": "Male",
"question": {
"QST_ID": 11,
"QST_display_text": {
"raw": "Gender",
"rendered": "<p>Gender</p>\n"
}
}
},
{
"ANS_ID": 113,
"ANS_value": [
"Double Occupancy"
],
"question": {
"QST_ID": 12,
"QST_display_text": {
"raw": "Room Type",
"rendered": "<p>Room Type</p>\n"
}
}
},
{
"ANS_ID": 114,
"ANS_value": "None",
"question": {
"QST_ID": 13,
"QST_display_text": {
"raw": "Who would you like to share your room with?",
"rendered": "<p>Who would you like to share your room with?</p>\n"
}
}
}
]
}
]
}
]
我在项目中拥有的C#等效项是here。
我的简单C#对象如下:
public class Attendee : RealmObject
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Type { get; set; }
public int RegistrationId { get; set; }
public string RegistrationCode { get; set; }
public string RegistrationStatus { get; set; }
public DateTimeOffset RegistrationDate { get; set; }
public DateTimeOffset RegistrationDateGMT { get; set; }
public long EventId { get; set; }
public long TicketId { get; set; }
public long GroupCount { get; set; }
public long TransactionId { get; set; }
public string TransactionStatus { get; set; }
public long PaymentId { get; set; }
public string PaymentStatus { get; set; }
public string CheckedIn { get; set; }
public Attendee()
{
}
}
我尝试了不同的方法,例如ForMember和ForPath。我也尝试过将内部对象映射到简单的对象,但仍然收到以下错误:
AutoMapper.AutoMapperConfigurationException:
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters
===================================================
AutoMapper created this type map for you, but your types cannot be mapped using the current configuration.
Char -> Attendee (Destination member list)
System.Char -> Organizer.Models.Attendees.Attendee (Destination member list)
Unmapped properties:
Id
FirstName
LastName
FullName
Email
Phone
Type
RegistrationId
RegistrationCode
RegistrationStatus
RegistrationDate
RegistrationDateGMT
EventId
TicketId
GroupCount
TransactionId
TransactionStatus
PaymentId
PaymentStatus
CheckedIn
我非常感谢我在映射它们方面所能提供的帮助。
谢谢
编辑:
在同一项目中,我必须遵循JSON和C#对象以及映射配置,尽管对象不同,但是映射工作得很好。
EE4 REST API中的JSON对象
[
{
"EVT_ID": 10584,
"EVT_name": "Mamboland Milano 2019",
"status": {
"raw": "publish",
"pretty": "Published"
},
"EVT_visible_on": "2019-01-26T16:11:04",
"datetimes": [
{
"DTT_ID": 1,
"DTT_EVT_start": "2019-04-04T12:00:00",
"DTT_EVT_end": "2019-04-08T04:00:00"
}
]
}
]
我的C#对象
public class Event
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string Status { get; set; }
public int DateTimeID { get; set; }
public DateTimeOffset Start { get; set; }
public DateTimeOffset End { get; set; }
public string VLogo { get; set; }
public string HLogo { get; set; }
public Event()
{
}
}
以上的Automapper映射配置:
#region Event Mappings
//Inner Class Status
CreateMap<DTOs.Status, Models.Event>()
.ForMember(d => d.Status, o => o.MapFrom(s => s.Pretty))
.ReverseMap();
//Inner Class Datetime
CreateMap<DTOs.Datetime, Models.Event>()
.ForMember(d => d.DateTimeID, o => o.MapFrom(s => s.Id))
.ForMember(d => d.Start, o => o.MapFrom(s => s.Start))
.ForMember(d => d.End, o => o.MapFrom(s => s.End))
.ReverseMap();
//Outer Class
CreateMap<DTOs.Event, Models.Event>()
.ForMember(d => d.Id, o => o.MapFrom(s => s.Id))
.ForMember(d => d.Name, o => o.MapFrom(s => s.Name))
.ForMember(d => d.City, o => o.Ignore())
.ForMember(d => d.Status, o => o.MapFrom(s => s.Status.Pretty))
.ForMember(d => d.DateTimeID, o => o.MapFrom(s => s.Datetimes.ToList().FirstOrDefault().Id))
.ForMember(d => d.Start, o => o.MapFrom(s => s.Datetimes.ToList().FirstOrDefault().Start))
.ForMember(d => d.End, o => o.MapFrom(s => s.Datetimes.ToList().FirstOrDefault().End))
.ForMember(d => d.VLogo, o => o.Ignore())
.ForMember(d => d.Id, o => o.Ignore())
.ReverseMap();
#endregion
如您所见,我必须先映射内部类,然后再映射外部类,这样做,我仅通过映射就可以生成c#对象。