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装换成对象

相关推荐
陈平安Java and C1 小时前
MyBatisPlus
java
秋野酱2 小时前
如何在 Spring Boot 中实现自定义属性
java·数据库·spring boot
Bunny02122 小时前
SpringMVC笔记
java·redis·笔记
feng_blog66883 小时前
【docker-1】快速入门docker
java·docker·eureka
code_shenbing3 小时前
基于 WPF 平台使用纯 C# 制作流体动画
开发语言·c#·wpf
code_shenbing3 小时前
基于 WPF 平台实现成语游戏
游戏·c#·wpf
枫叶落雨2224 小时前
04JavaWeb——Maven-SpringBootWeb入门
java·maven
m0_748232395 小时前
SpringMVC新版本踩坑[已解决]
java
码农小灰5 小时前
Spring MVC中HandlerInterceptor和Filter的区别
java·spring·mvc
乔木剑衣5 小时前
Java集合学习:HashMap的原理
java·学习·哈希算法·集合