我創建了一個Xamarin應用程序,其API調用由Refit和其他Paul Betts庫管理,並且正在查看將對象集合序列化爲Json屬性數組。Json.Net將collection集合成命名屬性
我的問題是,如何使用Json.Net將MemberBooking類型的MemberSlot對象的集合序列化到所需的Json中?
對象的名稱始終爲數字1 - > 4,但部分或全部可能不存在。
問題
難道我只是在MemberBooking對象列表屬性更改爲一個字典,並適當填充字符串鍵?
對象層次結構
public class MemberBookingRequest
{
[JsonProperty("member_booking_request")]
public MemberBooking Booking { get; set; }
}
public class MemberBooking
{
[JsonProperty("course_id")]
public int CourseId { get; set; }
[JsonProperty("date")]
public string TeeDate { get; set; }
[JsonProperty("time")]
public string TeeTime { get; set; }
[JsonProperty("slots")]
public List<MemberSlot> Slots { get; set; }
}
public class MemberSlot
{
[JsonIgnore]
public int Id { get; set; }
[JsonProperty("type")]
public BookingType Type { get; set; }
[JsonProperty("holes")]
public int Holes { get; set; }
[JsonProperty("user_id")]
public int MemberId { get; set; }
}
當前的Json
{
"member_booking_request":{
"course_id":1,
"date":"2016-09-29",
"time":"09:00",
"slots":[
{
"type":"Member",
"holes":18,
"user_id":110
},
{
"type":"Member",
"holes":18,
"user_id":111
},
{
"type":"Member",
"holes":18,
"user_id":112
},
{
"type":"Member",
"holes":18,
"user_id":117
]
}
}
}
期望的Json
{
"member_booking_request":{
"course_id":1,
"date":"2016-09-29",
"time":"09:00",
"slots":{
"1":{
"type":"Member",
"holes":18,
"user_id":110
},
"2":{
"type":"Member",
"holes":18,
"user_id":111
},
"3":{
"type":"Member",
"holes":18,
"user_id":112
},
"4":{
"type":"Member",
"holes":18,
"user_id":117
}
}
}
}
您顯示「所需的json」,但如果你表現出「當前的json」太多它會更加美好! – Aybe