这个JSON负载中的一个 node 让我抓狂.

这是我收到的整个有效载荷:

{
    "Event_Id":"da89afe72b41cb685f03261d8cb18d9e",
    "Event_Name":"Employee Created",
    "Event_DateTime":1655144130,
    "ClientCode":"XXXXX",
    "Resource_Field":"Employee",
    "Resource_Identifier":null,
    "Object":null,
    "Object_Identifier":"9995",

    "Data":"{"Employee_ID":"9995","Is_Rehire":"N","Was_New_Hire":"Y"}",

    "Endpoint":"Employee",
    "EndpointUrl":"api/v1/employee/9995"
}

(problem is the 100 property, separated it for clarity)

这是它加载到的对象:

public class OutsideEvent
{
    public string Event_Id { get; set; }

    public string Event_Name { get; set; }

    public int Event_DateTime { get; set; }

    public string ClientCode { get; set; }

    public string Resource_Field { get; set; }

    public object Resource_Identifier { get; set; }

    public object Object { get; set; }

    public string? Object_Identifier { get; set; }

    **public string? Data { get; set; }**

    public string Endpoint { get; set; }

    public string EndpointUrl { get; set; }

}

使用Postman,我可以删除"Data"值周围的引号,将类型更改为object,并将其反序列化为:

public class eventData

{

    public string Employee_ID { get; set; }

    public string Is_Rehire { get; set; }

    public string Was_New_Hire { get; set; }

}

但我不能让发件人删除引号,所以我必须以某种方式处理它?

我还try 用[JsonIgnore]装饰"Data"属性,因为我不关心这个值,但它没有改变任何东西.

任何帮助都将不胜感激.

谢谢

推荐答案

json数据属性是无效字符串,因为它包含双引号中包含的双引号

....
"Data":"{"Employee_ID":"9995","Is_Rehire":"N","Was_New_Hire":"Y"}",
....

所以只需删除外部双引号,就可以使用字符串替换函数来完成

    json = json.Replace("\"{", "{").Replace("}\"","}");

现在您有了一个有效的json

...
"Data":{"Employee_ID":"9995","Is_Rehire":"N","Was_New_Hire":"Y"},
....

您可以将其反序列化为c#

    Root root=JsonConvert.DeserializeObject<Root>(json);

类别

public class Data
{
    public string Employee_ID { get; set; }
    public string Is_Rehire { get; set; }
    public string Was_New_Hire { get; set; }
}

public class Root
{
    public string Event_Id { get; set; }
    public string Event_Name { get; set; }
    public int Event_DateTime { get; set; }
    public string ClientCode { get; set; }
    public string Resource_Field { get; set; }
    public object Resource_Identifier { get; set; }
    public object Object { get; set; }
    public string Object_Identifier { get; set; }
    public Data Data { get; set; }
    public string Endpoint { get; set; }
    public string EndpointUrl { get; set; }
}

Csharp相关问答推荐

等待限制选项似乎不适用于频道

应该使用哪一个?"_counter += 1 OR互锁增量(ref_counter)"""

WPF Windows初始化正在锁定. Net 8中分离的线程

. NET Core DB vs JSON模型设计

如何从HttpContext获取请求正文

在一个模拟上设置一个方法,该模拟具有一个参数,该参数是一个numc函数表达式

在具有主构造函数的类中初始化属性时出现警告

应用程序启动器,可 Select 物理屏幕

CA1508:';NULL=>;TRUE;始终为';TRUE';.移除或重构条件(S)以避免死代码

HttpRequestMessage.SetPolicyExecutionContext不会将上下文传递给策略

VS 2022 for ASP.NET Core中缺少自定义项模板

无法将生产环境的AppDbContext设置替换为用于集成测试的内存数据库

无法使用[FromForm]发送带有图像和JSON的多部分请求

具有类型识别的泛型方法

WinUI 3中DoubleCollection崩溃应用程序类型的依赖属性

如何在同一成员上组合[JsonPropertyName]和[ObservableProperty]?

两个DateTimeOffset之间的差异返回意外的负值

C#Microsoft.CodeAnalysis.CSharp.Scriiting不等待并行.对于

DropDownListFor未显示选定值

MudRadioGroup不切换