[C#]JSON反序列化对象--解析JSON文本

2022-06-13 18:28:55 浏览数 (1)

一直在用第三方的Json工具,现在才发现Json.net原版的就很好用。

并且借助JSON转C#实体类工具,几分钟就可以完成解析代码。

比如目前正在做的一个通讯测试,接收的信息如下:

代码语言:javascript复制
{
  "Content": {
    "__startTime": "2022-05-14 16:46:46",
    "__name": "slave1",
    "__description": "安装在泵车2;研华4117模块",
    "__config": "[ModbusRtu] [ModRtu/slave1] [Com3:9600-8-N-1]",
    "__success": 35865,
    "__failed": 0,
    "__activeTime": "2022-05-14 20:45:55",
    "__captureSpendTime": 372,
    "__deviceStatus": true,
    "__requestEnable": true,
    "__onlineTime": "2022-05-14 16:46:48",
    "__failedMsg": "",
    "__alarmCount": {
      "AlarmCount": 0,
      "Hint": 0,
      "Warn": 0,
      "Error": 0,
      "Fatal": 0
    },
    "三参仪压力": 2222,
    "三参仪流量_mA信号": 28493
  },
  "IsSuccess": true,
  "Message": "成功",
  "ErrorCode": 0
}

直接根据JSon转实体类如下

代码语言:javascript复制
public class __alarmCount
{
  /// <summary>
  /// 
  /// </summary>
  public int AlarmCount { get; set; }
  /// <summary>
  /// 
  /// </summary>
  public int Hint { get; set; }
  /// <summary>
  /// 
  /// </summary>
  public int Warn { get; set; }
  /// <summary>
  /// 
  /// </summary>
  public int Error { get; set; }
  /// <summary>
  /// 
  /// </summary>
  public int Fatal { get; set; }
}
public class ContentBase
{
  /// <summary>
  /// 
  /// </summary>
  public string __startTime { get; set; }
  /// <summary>
  /// 
  /// </summary>
  public string __name { get; set; }
  /// <summary>
  /// 安装在泵车2;研华4117模块
  /// </summary>
  public string __description { get; set; }
  /// <summary>
  /// 
  /// </summary>
  public string __config { get; set; }
  /// <summary>
  /// 
  /// </summary>
  public int __success { get; set; }
  /// <summary>
  /// 
  /// </summary>
  public int __failed { get; set; }
  /// <summary>
  /// 
  /// </summary>
  public string __activeTime { get; set; }
  /// <summary>
  /// 
  /// </summary>
  public int __captureSpendTime { get; set; }
  /// <summary>
  /// 
  /// </summary>
  public string __deviceStatus { get; set; }
  /// <summary>
  /// 
  /// </summary>
  public string __requestEnable { get; set; }
  /// <summary>
  /// 
  /// </summary>
  public string __onlineTime { get; set; }
  /// <summary>
  /// 
  /// </summary>
  public string __failedMsg { get; set; }
  /// <summary>
  /// 
  /// </summary>
  public __alarmCount __alarmCount { get; set; }
}
public class Content : ContentBase
{

  /// <summary>
  /// 
  /// </summary>
  public int 三参仪压力 { get; set; }
  /// <summary>
  /// 
  /// </summary>
  public int 三参仪流量_mA信号 { get; set; }
  public int 三参仪流量 { get; set; }
  /// <summary>
  /// 
  /// </summary>
  public int 三参仪密度 { get; set; }
}

public class Root
{
  /// <summary>
  /// 
  /// </summary>
  public Content Content { get; set; }
  /// <summary>
  /// 
  /// </summary>
  public string IsSuccess { get; set; }
  /// <summary>
  /// 成功
  /// </summary>
  public string Message { get; set; }
  /// <summary>
  /// 
  /// </summary>
  public int ErrorCode { get; set; }
}

运行结果如下:

总结:Json.NET已经实现了属性自动映射,很强大很好用。

以前的解析方法就不贴上来了,想想都觉得有点脱了裤子放屁。。。。。

官方例子。

反序列化对象 (newtonsoft.com)

This sample deserializes JSON to an object.

Types

Copy

代码语言:javascript复制
代码语言:javascript复制
public class Account
{
    public string Email { get; set; }
    public bool Active { get; set; }
    public DateTime CreatedDate { get; set; }
    public IList<string> Roles { get; set; }
}

Usage

Copy

代码语言:javascript复制
代码语言:javascript复制
string json = @"{
  'Email': 'james@example.com',
  'Active': true,
  'CreatedDate': '2013-01-20T00:00:00Z',
  'Roles': [
    'User',
    'Admin'
  ]
}";
代码语言:javascript复制
Account account = JsonConvert.DeserializeObject<Account>(json);
Console.WriteLine(account.Email);
// james@example.com

0 人点赞