天天看點

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

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

并且借助JSON轉C#實體類工具,幾分鐘就可以完成解析代碼。

比如目前正在做的一個通訊測試,接收的資訊如下:

{
  "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轉實體類如下

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

複制

運作結果如下:

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

總結:Json.NET已經實作了屬性自動映射,很強大很好用。

以前的解析方法就不貼上來了,想想都覺得有點脫了褲子放屁。。。。。

官方例子。

反序列化對象 (newtonsoft.com)

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

This sample deserializes JSON to an object.

Types

Copy

複制

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

複制

string json = @"{
  'Email': '[email protected]',
  'Active': true,
  'CreatedDate': '2013-01-20T00:00:00Z',
  'Roles': [
    'User',
    'Admin'
  ]
}";           

複制

Account account = JsonConvert.DeserializeObject<Account>(json);
Console.WriteLine(account.Email);
// [email protected]           

複制