C#JSON反序列化 [英] C# JSON deserialisation

查看:96
本文介绍了C#JSON反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{
Description:此注册表定义了管理处理器的事件。,

Id:Events.json,

消息:{

AEPSecureEraseFailed:{

描述:英特尔Optane DC持久存储器的安全擦除失败。,

消息:英特尔Optane DC持久内存的安全擦除失败,

ParamTypes:[string,string,string],

解决方案:重试安全删除。如果问题仍然存在,请联系HPE支持。,

严重性:严重

},

AdapterConfigurationChange:{

描述:指定的适配器已进行配置更改。,

消息:适配器在插槽%1中有一个配置更改。,

分辨率:无,

严重性:确定

},

FanRemoved:{

描述:粉丝已被移除。,

消息:粉丝重新开始移动。,

分辨率:无,

严重程度:警告

}

},

名称:事件登记处

}



我是什么尝试过:



公共类AEPSecureEraseFailed

{

公共字符串说明{get;组; } $ / $
public string Message {get;组; }

public List< string> ParamTypes {get;组; }

public string Resolution {get;组; }

public string Severity {get;组; }

}



公共类AdapterConfigurationChange

{

公共字符串描述{得到;组; } $ / $
public string Message {get;组; }

public string Resolution {get;组; }

public string Severity {get;组; }

}



公共类FanRemoved

{

公共字符串描述{得到;组; } $ / $
public string Message {get;组; }

public string Resolution {get;组; }

public string Severity {get;组; }

}



公共类消息

{

public AEPSecureEraseFailed AEPSecureEraseFailed {得到;组; } $ / $
public AdapterConfigurationChange AdapterConfigurationChange {get;组; }

public FanRemoved FanRemoved {get;组; }

}



公共类RootObject

{

公共字符串描述{得到;组; }

public string Id {get;组; }

公共消息消息{get;组; }

public string Name {get;组; }

}



除了以下方法之外还有其他反序列化方法:

公共类消息
{

public AEPSecureEraseFailed AEPSecureEraseFailed {get;组; } $ / $
public AdapterConfigurationChange AdapterConfigurationChange {get;组; }

public FanRemoved FanRemoved {get;组; }

}

这样我们就不必为其他256个左侧事件编写相同的行

解决方案

< blockquote>我想推荐

json2csharp - 从json生成c#类 [ ^ ]

此站点将从任何有效的JSON字符串生成C#类。

无需手动编写相应的C#类。


Json.NET [ ^ ]也可能有用。


当你开始时会对JSON数据进行序列化,本文将回答这个问题以及您将拥有的许多其他问题:在C#中使用JSON& VB [ ^ ]

{ "Description": "This registry defines the events for the management processor.",
"Id": "Events.json",
"Messages": {
"AEPSecureEraseFailed": {
"Description": "Secure Erase of Intel Optane DC Persistent Memory has failed.",
"Message": "Secure Erase of Intel Optane DC Persistent Memory has failed",
"ParamTypes": ["string", "string", "string"],
"Resolution": "Retry Secure Erase. Please contact HPE Support if issue persists.",
"Severity": "Critical"
},
"AdapterConfigurationChange": {
"Description": "The specified adapter has had a configuration change.",
"Message": "The adapter in slot %1 has had a configuration change.",
"Resolution": "None",
"Severity": "Ok"
},
"FanRemoved": {
"Description": "The fan is removed.",
"Message": "The fan is removed.",
"Resolution": "None",
"Severity": "Warning"
}
},
"Name": "Event Registry"
}

What I have tried:

public class AEPSecureEraseFailed
{
public string Description { get; set; }
public string Message { get; set; }
public List<string> ParamTypes { get; set; }
public string Resolution { get; set; }
public string Severity { get; set; }
}

public class AdapterConfigurationChange
{
public string Description { get; set; }
public string Message { get; set; }
public string Resolution { get; set; }
public string Severity { get; set; }
}

public class FanRemoved
{
public string Description { get; set; }
public string Message { get; set; }
public string Resolution { get; set; }
public string Severity { get; set; }
}

public class Messages
{
public AEPSecureEraseFailed AEPSecureEraseFailed { get; set; }
public AdapterConfigurationChange AdapterConfigurationChange { get; set; }
public FanRemoved FanRemoved { get; set; }
}

public class RootObject
{
public string Description { get; set; }
public string Id { get; set; }
public Messages Messages { get; set; }
public string Name { get; set; }
}

Is there any other way of deserialising other than:
public class Messages
{
public AEPSecureEraseFailed AEPSecureEraseFailed { get; set; }
public AdapterConfigurationChange AdapterConfigurationChange { get; set; }
public FanRemoved FanRemoved { get; set; }
}
so that we do not have to write the same lines for the other 256 left events

解决方案

I would like to recommend
json2csharp - generate c# classes from json[^]
This site will generate a C# class from any valid JSON string.
There is no need to write a corresponding C# class by hand.


Json.NET[^] may also be useful.


As you're starting out with serialization of JSON data, this article answers this question and many others that you will have: Working with JSON in C# & VB[^]


这篇关于C#JSON反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆