1

我正在使用由Refit和其他Paul Betts库管理的 API 调用创建一个 Xamarin 应用程序,并且正在研究将对象集合序列化为 Json 属性数组。

我的问题是如何使用 Json.Net 将 MemberBooking 类型中的 MemberSlot 对象集合序列化为所需的 Json?

对象的名称将始终为数字 1 -> 4,但部分或全部可能不存在。

问题

我可以将 MemberBooking 对象中的 List 属性更改为 Dictionary 并适当地填充字符串键吗?

对象层次结构

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

2 回答 2

2

您必须更改创建 Slots 属性的方式。在填充 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; }
    }
于 2016-10-10T15:38:11.680 回答
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; }
    }
}
于 2016-10-10T15:49:17.687 回答