在C#中使用嵌套List反序列化JSON

时间:2016-12-10 09:08:40

标签: c# json json-deserialization

如何在C#中使用嵌套List反序列化JSON,我的活动始终为null。 请帮我。无法到达我错的地方。任何帮助都会非常感激。

我正在尝试反序列化代码,但没有获取活动。我的项目是:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;

public class Program
{
    public static void Main()
    {
        string json = @"
            {
  ""Title"": ""test"",
  ""Id"": ""SR21576"",
  ""CreatedDate"": ""2016-12-10T09:21:06+03:30"",
  ""DisplayName"": ""SR21576 : test"",
  ""Description"": ""10"",
  ""AlternateContactMethod"": null,
  ""AffectedUser"": null,
  ""AssignedToUser"": ""XXX\\BB Team"",
  ""CustomerCode"": ""00000000-0000-0000-0000-000000000000"",
  ""CustomerName"": null,
  ""ContractId"": ""00000000-0000-0000-0000-000000000000"",
  ""ContractTitle"": null,
  ""ContractDetailsId"": ""00000000-0000-0000-0000-000000000000"",
  ""ContractDetailsTitle"": null,
  ""CRMLink"": null,
  ""LeadId"": ""00000000-0000-0000-0000-000000000000"",
  ""LeadTitle"": null,
  ""Urgency"": ""b02d9277-a9fe-86f1-e95e-0ba8cd4fd075"",
  ""Priority"": ""1e070214-693f-4a19-82bb-b88ee6362d98"",
  ""Source"": ""57e11419-ac27-a6fa-97f1-0ec2a9722807"",
  ""Area"": ""3c5e6037-b057-5745-4d89-e95f67d3236b"",
  ""SupportGroup"": ""ae536997-8124-85e7-496e-225b7991a450"",
  ""Attachments"": [ ],
  ""Activities"": [
    {
      ""<Id>k__BackingField"": ""ffe60a69-f3c8-adbe-9bbd-4050d766cc81"",
      ""<ActivityID>k__BackingField"": ""MA21578"",
      ""<Title>k__BackingField"": ""a"",
      ""<Status>k__BackingField"": ""Ready"",
      ""<TypeOfActivity>k__BackingField"": 1
    },
    {
      ""<Id>k__BackingField"": ""91286e74-6a39-964f-040f-5daf99e84792"",
      ""<ActivityID>k__BackingField"": ""RA21579"",
      ""<Title>k__BackingField"": ""b"",
      ""<Status>k__BackingField"": ""Ready"",
      ""<TypeOfActivity>k__BackingField"": 0
    },
    {
      ""<Id>k__BackingField"": ""9754f1a1-d37b-265a-087a-f6688808dbeb"",
      ""<ActivityID>k__BackingField"": ""MA21577"",
      ""<Title>k__BackingField"": ""c"",
      ""<Status>k__BackingField"": ""Active"",
      ""<TypeOfActivity>k__BackingField"": 1
    },
    {
      ""<Id>k__BackingField"": ""d959cac5-cb78-a3fb-09e2-76da1b5381b2"",
      ""<ActivityID>k__BackingField"": ""MA21580"",
      ""<Title>k__BackingField"": ""d"",
      ""<Status>k__BackingField"": ""Ready"",
      ""<TypeOfActivity>k__BackingField"": 1
    }
  ],
  ""Status"": ""InProgress""
}";


        ServiceRequest items = JsonConvert.DeserializeObject<ServiceRequest>(json);

        Console.WriteLine("Area: " + items.Area);
        Console.WriteLine("CRMLink: " + items.CRMLink);

        Console.WriteLine("Number of Activities: " + items.Activities.Count);

        int i = 0;
        //Activities are null why???????
        foreach (var type in items.Activities)
        {
            i++;
            Console.WriteLine("Activities item number " + i);
            Console.WriteLine("ActivityID: " + type.ActivityID);
            Console.WriteLine("Id: " + type.Id);
            Console.WriteLine("Status: " + type.Status);
            Console.WriteLine("Title: " + type.Title);
            Console.WriteLine("TypeOfActivity: " + type.TypeOfActivity);
            Console.WriteLine();
        }
    }
}

[Serializable]
public class Activity
{
    public Guid Id { get; set; }
    public String ActivityID { get; set; }
    public String Title { get; set; }
    public String Status { get; set; }

    public ActivityType TypeOfActivity { get; set; }
}

[Serializable]
public enum ActivityType
{
    ReviewActvity,
    ManualActivity
}

[Serializable]
public class Attachment
{
    [Required]
    public string FileName { get; set; }
    [Required]
    public string ServerFileName { get; set; }
    public bool ToRemove { get; set; }
    [Required]
    public string AttachedByUser { get; set; }

}

[Serializable]
public class ServiceRequest
{
    [Required]
    public string Title { get; set; }
    public String Id { get; set; }
    public DateTime CreatedDate { get; set; }
    public string DisplayName { get; set; }
    [Required]
    public string Description { get; set; }
    public string AlternateContactMethod { get; set; }
    public string AffectedUser { get; set; }
    public string AssignedToUser { get; set; }
    public Guid CustomerCode { get; set; }
    public string CustomerName { get; set; }
    public Guid ContractId { get; set; }
    public string ContractTitle { get; set; }
    public Guid ContractDetailsId { get; set; }
    public string ContractDetailsTitle { get; set; }
    public string CRMLink { get; set; }
    public Guid LeadId { get; set; }
    public string LeadTitle { get; set; }
    public Guid Urgency { get; set; }
    public Guid Priority { get; set; }
    public Guid Source { get; set; }
    public Guid Area { get; set; }
    public Guid SupportGroup { get; set; }
    public List<Attachment> Attachments { get; set; }
    public List<Activity> Activities { get; set; }
    public string Status { get; set; }
}

2 个答案:

答案 0 :(得分:5)

最初为Activities集合生成JSON的人选择序列化对象的字段。由于该类完全由自动实现的属性组成,因此您获得了secret backing fields

要强制Json.NET反序列化Activity的公共和私有字段而不是其属性,请使用[JsonObject(MemberSerialization.Fields)]标记该类型:

[Serializable]
[JsonObject(MemberSerialization.Fields)]
public class Activity
{
    public Guid Id { get; set; }
    public String ActivityID { get; set; }
    public String Title { get; set; }
    public String Status { get; set; }

    public ActivityType TypeOfActivity { get; set; }
}

或者,修复最初生成JSON的代码,以优先于字段序列化属性。这似乎是一种有意识的选择,因为根类型ServiceRequest的属性已序列化,而不是其字段。

示例fiddle

答案 1 :(得分:0)

方法1 :从[Serializable]类中删除Activity属性。

如果不是绝对必要,删除将阻止默认DataContractJsonSerializer生成额外标记。

方法2 :配置序列化程序以忽略[Serializable]属性:

config.Formatters.JsonFormatter.SerializerSettings.ContractResolver =
new DefaultContractResolver { IgnoreSerializableAttribute = true };

Reference