Json.Net 将集合序列化为命名属性

Json.Net serialise collection into named attributes

我正在创建一个 Xamarin 应用程序,其中 API 调用由 Refit and other Paul Betts 库管理,并且正在考虑将对象集合序列化为 Json 属性数组。

我的问题是如何使用 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
         }
      }
   }
}

您必须更改创建插槽的方式 属性。在填充 Slots 字典时分配 MemberSlot.Id 作为键,MemberSlot 本身作为值。

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

此示例将提供您想要的 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; }
    }
}