gpt4 book ai didi

c# - 在 C# 中使用嵌套列表反序列化 JSON

转载 作者:行者123 更新时间:2023-12-03 03:02:43 25 4
gpt4 key购买 nike

如何在 C# 中使用嵌套列表反序列化 JSON,我的 Activity 始终为 null。请帮我。无法找到我出错的地方。任何帮助将不胜感激。

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

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

最佳答案

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

要强制 Json.NET 反序列化 Activity 的公共(public)和私有(private)字段而不是其属性,请使用 [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 .

关于c# - 在 C# 中使用嵌套列表反序列化 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41073898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com