【问题标题】:Deserialising json string to List<T>将 json 字符串反序列化为 List<T>
【发布时间】:2016-10-11 12:10:37
【问题描述】:

我有一个以表单形式输出 JSON 的 Web 服务

{"AppointmentList":[{"AppointmentList":{"id":"1","MeetingId":"1","MeetingName":"Test Meeting 1","Length":"90","Room":"B2C","DateTimeFrom":"1st Sept 2016","Venue":"The old pub","DateCreated":"2016-08-30 00:00:00","DateDue":"2016-09-01 00:00:00","UserId":"JohnsonPa"}},{"AppointmentList":{"id":"2","MeetingId":"2","MeetingName":"Test Meeting 2","Length":"60","Room":"B2C","DateTimeFrom":"11th Sept 2016","Venue":"The old pub","DateCreated":"2016-09-01 00:00:00","DateDue":"2016-09-12 00:00:00","UserId":"JohnsonPa"}...}]}

我正在尝试将其反序列化到列表中。通常,我会有一个基类,其中包含一个属性 List AppointmentList {get; set;} 但是,这意味着我不能使用类型 T 并且每个类都需要一堆重复的代码。

我当然可以使用 public List Data {get; 属性创建 BaseClass set;} 但是,因为 JSON 不会反序列化为 Data(名称不正确),并且 JSON PropertyName 不能设置为派生自 typeof(T).ToString() 的类名。

有没有办法在不使用大量重复代码的情况下实现我想要做的事情?

我尝试将反序列化器转换为 JArray 并从中创建一个读取器,但这会引发异常。

【问题讨论】:

  • 不知道为什么为这个对象创建一个具体的类会阻止你使用泛型?实际上,最好创建一个与反序列化对象匹配的视图模型
  • 为什么我需要一个视图模型——它是纯粹的数据,所以可以在模型中处理。
  • 只是术语 - 如果您愿意,可以将其称为 DTO。重点是,为了反序列化它,最好告诉你正在使用什么机制来反序列化。尤其是当你传递这个对象时。仍然没有回答为什么这会影响使用泛型
  • 为什么要将 AppointmentList 反序列化为一个名为 Data 的属性?为什么不能叫 AppointmentList?

标签: c# json json.net


【解决方案1】:

我不确定这是否正是您所需要的,但也许这样的事情会起作用?它允许您成功反序列化为 JArray,就像您在问题结束时所说的那样。

JArray result = JsonConvert.DeserializeObject<dynamic>(json).AppointmentList;

【讨论】:

    【解决方案2】:

    这里如何将其转换为List&lt;object&gt;

    dynamic data = JsonConvert.DeserializeObject(json);
    JArray array = data.AppointmentList;
    
    List<object> objectList = array.ToObject<List<object>>();
    

    【讨论】:

    • 谢谢。唯一的问题是我传入 T 这是类,因此 data.T 不会给出任何东西。
    • @Nodoid 如果我理解你正确地写了类名而不是对象。或者你在某个泛型类中有这个 json,并且你想从这个泛型方法返回 List。这种情况下如果是T:object,写List应该没问题吧?
    【解决方案3】:

    泛型有什么问题?如果你想要一个无模式的数据结构,请使用 JObject 或动态的,如果不是,你可以试试这个。

     class Program
        {
            public const string json = @"{""AppointmentList"":[{""AppointmentList"":{""id"":""1"",""MeetingId"":""1"",""MeetingName"":""Test Meeting 1"",""Length"":""90"",""Room"":""B2C"",""DateTimeFrom"":""1st Sept 2016"",""Venue"":""The old pub"",""DateCreated"":""2016-08-30 00:00:00"",""DateDue"":""2016-09-01 00:00:00"",""UserId"":""JohnsonPa""}},{""AppointmentList"":{""id"":""2"",""MeetingId"":""2"",""MeetingName"":""Test Meeting 2"",""Length"":""60"",""Room"":""B2C"",""DateTimeFrom"":""11th Sept 2016"",""Venue"":""The old pub"",""DateCreated"":""2016-09-01 00:00:00"",""DateDue"":""2016-09-12 00:00:00"",""UserId"":""JohnsonPa""}}]}";
    
            static void Main(string[] args)
            {
                var items = Newtonsoft.Json.JsonConvert.DeserializeObject<AppointmentItemList<Meeting1>>(json).GetList();
    
                var items2 = Newtonsoft.Json.JsonConvert.DeserializeObject<AppointmentItemList<Meeting2>>(json).GetList();
    
                Console.ReadLine();
            }
    
            public class AppointmentItemList<T> 
            {
                public List<AppointmentItem> AppointmentList { get; set; }
    
                public class AppointmentItem 
                {
                    public T AppointmentList { get; set; }
                }
    
                public IList<T> GetList()
                {
                    return AppointmentList.Select(al => al.AppointmentList).ToList();
                }
            }
    
            public class Meeting1 
            {
                [Newtonsoft.Json.JsonProperty("id")]
                public string Id { get; set; }
    
                public string MeetingName { get; set; }
            }
    
            public class Meeting2
            {
                [Newtonsoft.Json.JsonProperty("id")]
                public string Id { get; set; }
    
                public string Room { get; set; }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      相关资源
      最近更新 更多