c# Json转C#实体

1.Web Api获取 Json数据:

cs 复制代码
{ "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

转换后的实体类:

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ServerSide.Models
{
    //如果好用,请收藏地址,帮忙分享。
    public class Barcode
    {
        /// <summary>
        /// 
        /// </summary>
        public string ?BarcodeNo { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string ?WorkOrder { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string ?ItemNo { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int ?PlanQty { get; set; }
    }

    public class PartsItem
    {
        /// <summary>
        /// 机箱
        /// </summary>
        public string? PartDesc { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string ?PartSn { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string ?CreateDate { get; set; }
    }

    public class StationsItem
    {
        /// <summary>
        /// 
        /// </summary>
        public string ?LineDesc { get; set; }
        /// <summary>
        /// 前测
        /// </summary>
        public string ?WorkStationDesc { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string ?Status { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string ?CreateDate { get; set; }
    }

    public class Data
    {
        /// <summary>
        /// 
        /// </summary>
        public Barcode ?Barcode { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<PartsItem> ?Parts { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<StationsItem> ?Stations { get; set; }
    }

    public class Root
    {
        /// <summary>
        /// 
        /// </summary>
        public int ?code { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string ?message { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Data ?data { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string ?time { get; set; }
    }
}

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

cs 复制代码
        public bool GetBarcodeInfo(string strSN)
        {
            string retString = string.Empty;
            string url = $@"{this.webApiPort}MesApi/api/Product/GetBarcodeInfo";
            //List<string> MESMAC = new List<string>();
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?barcodeNo=" + strSN);
                request.Proxy = null;
                request.KeepAlive = false;
                request.Method = "GET";
                request.ContentType = "application/json; charset=UTF-8";
                request.AutomaticDecompression = DecompressionMethods.GZip;

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string encoding = response.ContentEncoding;
                if (encoding == null || encoding.Length < 1)
                {
                    encoding = "UTF-8"; //默认编码  
                }
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
                retString = reader.ReadToEnd();
                //读取SN 绑定数据
                // 添加代码
                Newtonsoft.Json.Linq.JObject resInfo = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(retString)!;
                //this.barcodeInfo = new Barcode();
                this.barcodeInfo = JsonConvert.DeserializeObject<Root>(resInfo.ToString())!; //JsonConvert.DeserializeObject<Barcode>(resInfo.ToString())!;
                //if (resInfo["code"]!.ToString()=="200")
                if (this.barcodeInfo.code.ToString()=="200")
                {
                    barCodeInfoEntity = new BarCodeInfoEntity();
                    barCodeInfoEntity.BarcodeNo = strSN;
                    barCodeInfoEntity.WorkOrder = this.barcodeInfo!.data!.Barcode!.WorkOrder!.ToString();
                    barCodeInfoEntity.ItemNo = this.barcodeInfo!.data!.Barcode!.ItemNo!.ToString();
                    barCodeInfoEntity.Total_Number = Convert.ToInt32(this!.barcodeInfo!.data!.Barcode!.PlanQty!.ToString());
                    
                    for(int i = 0; i <this!.barcodeInfo!.data!.Parts!.Count();i++)
                    {
                        if (this!.barcodeInfo!.data!.Parts![i]!.PartDesc!.ToString().Contains("主板"))
                            barCodeInfoEntity.PartSn = this!.barcodeInfo!.data!.Parts![i]!.PartSn!.ToString();
                        else if (this!.barcodeInfo!.data!.Parts![i]!.PartDesc!.ToString().Contains("硬盘"))
                            barCodeInfoEntity.Hddinfo = this!.barcodeInfo!.data!.Parts![i]!.PartSn!.ToString();
                        else if (this!.barcodeInfo!.data!.Parts![i]!.PartDesc!.ToString().Contains("内存条"))
                            barCodeInfoEntity.Memoryinfo = this!.barcodeInfo!.data!.Parts![i]!.PartSn!.ToString();
                        else if (this!.barcodeInfo!.data!.Parts![i]!.PartDesc!.ToString().Contains("MAC"))
                            barCodeInfoEntity.MacAddresss += this!.barcodeInfo!.data!.Parts![i]!.PartSn!.ToString() + ",";
                        else if (this!.barcodeInfo!.data!.Parts![i]!.PartDesc!.ToString().Contains("CMEI"))
                            barCodeInfoEntity.Cmei = this!.barcodeInfo!.data!.Parts![i]!.PartSn!.ToString();
                        else if (this!.barcodeInfo!.data!.Parts![i]!.PartDesc!.ToString().Contains("WIFI"))
                            barCodeInfoEntity.WIFI = this!.barcodeInfo!.data!.Parts![i]!.PartSn!.ToString();
                    }
                    if (barCodeInfoEntity != null)
                        barCodeInfoEntity.MacAddresss = barCodeInfoEntity?.MacAddresss?.Trim(',');
                    return true;
                }
                else
                {
                    this.err =this!.barcodeInfo!.message!.ToString();
                    return false;
                }
            }
            catch (Exception ex)
            {
                this.err = "Mes读取数据错误:" + ex.Message;
                return false;
            }
            
        }
        #endregion

Json转换实体的代码:

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

this.barcodeInfo = JsonConvert.DeserializeObject<Root>(resInfo.ToString())!;

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

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

相关推荐
wapicn993 分钟前
手机归属地查询Api接口,数据准确可靠
java·python·智能手机·php
hycccccch30 分钟前
Springcache+xxljob实现定时刷新缓存
java·后端·spring·缓存
wisdom_zhe43 分钟前
Spring Boot 日志 配置 SLF4J 和 Logback
java·spring boot·logback
揣晓丹1 小时前
JAVA实战开源项目:校园失物招领系统(Vue+SpringBoot) 附源码
java·开发语言·vue.js·spring boot·开源
于过1 小时前
Spring注解编程模型
java·后端
北随琛烬入1 小时前
Spark(10)配置Hadoop集群-集群配置
java·hadoop·spark
顽疲1 小时前
从零用java实现 小红书 springboot vue uniapp (11)集成AI聊天机器人
java·vue.js·spring boot·ai
Yan-英杰1 小时前
DeepSeek-R1模型现已登录亚马逊云科技
java·大数据·人工智能·科技·机器学习·云计算·deepseek
TDengine (老段)2 小时前
TDengine 中的日志系统
java·大数据·数据库·物联网·时序数据库·tdengine·iotdb
不想学习!!2 小时前
linux之进程控制
java·linux·服务器