2016-10-10 88 views
1

我創建了一個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 
     } 
     } 
    } 
} 
+0

您顯示「所需的json」,但如果你表現出「當前的json」太多它會更加美好! – Aybe

回答

2

你必須CH憤怒你創建Slots屬性的方式。將MemberSlot.Id指定爲鍵時,將MemberSlot本身作爲填充Slots字典時的值。

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 Dictionary<int,MemberSlot> Slots { get; set; } 
    } 
0

此示例會給你想要的JSON輸出,使用字典

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using Newtonsoft.Json; 

    namespace jsonconversion 
    { 
    public class Program 
    { 

     public static void Main(string[] args) 
     { 
      Dictionary<int,MemberSlot> slots=new Dictionary<int, MemberSlot>(); 
      MemberSlot slot1 = new MemberSlot() {Id = 1, Holes =2, MemberId =  1}; 
      slots.Add(1,slot1); 
      MemberBookingRequest mbr = new MemberBookingRequest 
      { 
       Booking = new MemberBooking() 
        {CourseId = 1, TeeDate ="",TeeTime = "",Slots = slots} 
      }; 
      string jd = JsonConvert.SerializeObject(mbr); 
      Console.WriteLine(jd); 
     } 
    } 



    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 Dictionary<int, MemberSlot> Slots { get; set; } 
    } 

    public class MemberSlot 
    { 
     [JsonIgnore] 
     public int Id { get; set; } 


     [JsonProperty("holes")] 
     public int Holes { get; set; } 

     [JsonProperty("user_id")] 
     public int MemberId { get; set; } 
    } 
}