Json.Net序列化集合到命名属性中

时间:2016-10-10 15:28:41

标签: c# json xamarin json.net refit

我正在使用由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
         }
      }
   }
}

2 个答案:

答案 0 :(得分: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; }
    }

答案 1 :(得分: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; }
    }
}