• c# Json转C#实体


    1.Web Api获取 Json数据

    { "code": 200, "message": "success", "data": { "Barcode": { "BarcodeNo": "YS5193465232200001", "WorkOrder": "N102304065", "ItemNo": "95.56.Z02.11000001", "PlanQty": 90 }, "Parts": [ { "PartDesc": "主板", "PartSn": "AL232300081", "CreateDate": "2023-05-30 10:09:15" }, { "PartDesc": "MAC2", "PartSn": "00073EABC43C", "CreateDate": "2023-05-30 10:09:15" }, { "PartDesc": "MAC", "PartSn": "00073EABC43C", "CreateDate": "2023-05-30 10:09:15" }, { "PartDesc": "硬盘", "PartSn": "323571903BA", "CreateDate": "2023-05-30 10:09:15" }, { "PartDesc": "内存条", "PartSn": "2312121901244", "CreateDate": "2023-05-30 10:09:15" } ], "Stations": [ { "LineDesc": "Line 03", "WorkStationDesc": "前测", "Status": "Pass", "CreateDate": "2023-05-30 17:14:06" }, { "LineDesc": "Line 03", "WorkStationDesc": "老化", "Status": "Pass", "CreateDate": "2023-06-05 11:03:30" }, { "LineDesc": "Line 01", "WorkStationDesc": "外观", "Status": "Pass", "CreateDate": "2023-06-06 19:37:17" }, { "LineDesc": "Line 03", "WorkStationDesc": "组装1", "Status": "Pass", "CreateDate": "2023-05-30 10:09:15" }, { "LineDesc": "Line 03", "WorkStationDesc": "包装3", "Status": "Pass", "CreateDate": "2023-06-13 19:54:32" } ] }, "time": "2023-10-28 23:06:22" }

    2.将Json转C#实体类网址:JSON转C#实体类-BeJSON.com

    转换后的实体类:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace ServerSide.Models
    7. {
    8. //如果好用,请收藏地址,帮忙分享。
    9. public class Barcode
    10. {
    11. ///
    12. ///
    13. ///
    14. public string ?BarcodeNo { get; set; }
    15. ///
    16. ///
    17. ///
    18. public string ?WorkOrder { get; set; }
    19. ///
    20. ///
    21. ///
    22. public string ?ItemNo { get; set; }
    23. ///
    24. ///
    25. ///
    26. public int ?PlanQty { get; set; }
    27. }
    28. public class PartsItem
    29. {
    30. ///
    31. /// 机箱
    32. ///
    33. public string? PartDesc { get; set; }
    34. ///
    35. ///
    36. ///
    37. public string ?PartSn { get; set; }
    38. ///
    39. ///
    40. ///
    41. public string ?CreateDate { get; set; }
    42. }
    43. public class StationsItem
    44. {
    45. ///
    46. ///
    47. ///
    48. public string ?LineDesc { get; set; }
    49. ///
    50. /// 前测
    51. ///
    52. public string ?WorkStationDesc { get; set; }
    53. ///
    54. ///
    55. ///
    56. public string ?Status { get; set; }
    57. ///
    58. ///
    59. ///
    60. public string ?CreateDate { get; set; }
    61. }
    62. public class Data
    63. {
    64. ///
    65. ///
    66. ///
    67. public Barcode ?Barcode { get; set; }
    68. ///
    69. ///
    70. ///
    71. public List ?Parts { get; set; }
    72. ///
    73. ///
    74. ///
    75. public List ?Stations { get; set; }
    76. }
    77. public class Root
    78. {
    79. ///
    80. ///
    81. ///
    82. public int ?code { get; set; }
    83. ///
    84. ///
    85. ///
    86. public string ?message { get; set; }
    87. ///
    88. ///
    89. ///
    90. public Data ?data { get; set; }
    91. ///
    92. ///
    93. ///
    94. public string ?time { get; set; }
    95. }
    96. }

    代码实现:引用库文件:using Newtonsoft.Json;

    1. public bool GetBarcodeInfo(string strSN)
    2. {
    3. string retString = string.Empty;
    4. string url = $@"{this.webApiPort}MesApi/api/Product/GetBarcodeInfo";
    5. //List MESMAC = new List();
    6. try
    7. {
    8. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?barcodeNo=" + strSN);
    9. request.Proxy = null;
    10. request.KeepAlive = false;
    11. request.Method = "GET";
    12. request.ContentType = "application/json; charset=UTF-8";
    13. request.AutomaticDecompression = DecompressionMethods.GZip;
    14. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    15. string encoding = response.ContentEncoding;
    16. if (encoding == null || encoding.Length < 1)
    17. {
    18. encoding = "UTF-8"; //默认编码
    19. }
    20. StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
    21. retString = reader.ReadToEnd();
    22. //读取SN 绑定数据
    23. // 添加代码
    24. Newtonsoft.Json.Linq.JObject resInfo = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(retString)!;
    25. //this.barcodeInfo = new Barcode();
    26. this.barcodeInfo = JsonConvert.DeserializeObject(resInfo.ToString())!; //JsonConvert.DeserializeObject(resInfo.ToString())!;
    27. //if (resInfo["code"]!.ToString()=="200")
    28. if (this.barcodeInfo.code.ToString()=="200")
    29. {
    30. barCodeInfoEntity = new BarCodeInfoEntity();
    31. barCodeInfoEntity.BarcodeNo = strSN;
    32. barCodeInfoEntity.WorkOrder = this.barcodeInfo!.data!.Barcode!.WorkOrder!.ToString();
    33. barCodeInfoEntity.ItemNo = this.barcodeInfo!.data!.Barcode!.ItemNo!.ToString();
    34. barCodeInfoEntity.Total_Number = Convert.ToInt32(this!.barcodeInfo!.data!.Barcode!.PlanQty!.ToString());
    35. for(int i = 0; i <this!.barcodeInfo!.data!.Parts!.Count();i++)
    36. {
    37. if (this!.barcodeInfo!.data!.Parts![i]!.PartDesc!.ToString().Contains("主板"))
    38. barCodeInfoEntity.PartSn = this!.barcodeInfo!.data!.Parts![i]!.PartSn!.ToString();
    39. else if (this!.barcodeInfo!.data!.Parts![i]!.PartDesc!.ToString().Contains("硬盘"))
    40. barCodeInfoEntity.Hddinfo = this!.barcodeInfo!.data!.Parts![i]!.PartSn!.ToString();
    41. else if (this!.barcodeInfo!.data!.Parts![i]!.PartDesc!.ToString().Contains("内存条"))
    42. barCodeInfoEntity.Memoryinfo = this!.barcodeInfo!.data!.Parts![i]!.PartSn!.ToString();
    43. else if (this!.barcodeInfo!.data!.Parts![i]!.PartDesc!.ToString().Contains("MAC"))
    44. barCodeInfoEntity.MacAddresss += this!.barcodeInfo!.data!.Parts![i]!.PartSn!.ToString() + ",";
    45. else if (this!.barcodeInfo!.data!.Parts![i]!.PartDesc!.ToString().Contains("CMEI"))
    46. barCodeInfoEntity.Cmei = this!.barcodeInfo!.data!.Parts![i]!.PartSn!.ToString();
    47. else if (this!.barcodeInfo!.data!.Parts![i]!.PartDesc!.ToString().Contains("WIFI"))
    48. barCodeInfoEntity.WIFI = this!.barcodeInfo!.data!.Parts![i]!.PartSn!.ToString();
    49. }
    50. if (barCodeInfoEntity != null)
    51. barCodeInfoEntity.MacAddresss = barCodeInfoEntity?.MacAddresss?.Trim(',');
    52. return true;
    53. }
    54. else
    55. {
    56. this.err =this!.barcodeInfo!.message!.ToString();
    57. return false;
    58. }
    59. }
    60. catch (Exception ex)
    61. {
    62. this.err = "Mes读取数据错误:" + ex.Message;
    63. return false;
    64. }
    65. }
    66. #endregion

    Json转换实体的代码:

     Newtonsoft.Json.Linq.JObject resInfo = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(retString)!;
    this.barcodeInfo = JsonConvert.DeserializeObject(resInfo.ToString())!;

    a>.实体转换成Json利用JsonConvert.SerializeObject 将Barcode转换成Json

    b>.Json转换成实体利用JsonConvert.DeserializeObject 将Json装换成对象

  • 相关阅读:
    【云原生| Docker】 部署 Django & mysql 项目
    CV攻城狮入门VIT(vision transformer)之旅——近年超火的Transformer你再不了解就晚了!
    【结构型模型】享元模式
    计算机毕业设计ssm工作室管理系统v186g系统+程序+源码+lw+远程部署
    通过conda创建纯净Python环境
    JAVAAPI实现血缘关系Rest推送到DataHub V0.12.1版本
    【LWM2M协议传输ADC到ONENET移动云】
    C#参数修饰符params
    一篇带你搞定⭐《生产环境JVM日志配置》⭐
    从源码看vue(v2.7.10)中的v-bind的原理
  • 原文地址:https://blog.csdn.net/u013934107/article/details/134097822