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

转换后的实体类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

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

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

    public class Data
    {
        /// 
        /// 
        /// 
        public Barcode ?Barcode { get; set; }
        /// 
        /// 
        /// 
        public List ?Parts { get; set; }
        /// 
        /// 
        /// 
        public List ?Stations { get; set; }
    }

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

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

        public bool GetBarcodeInfo(string strSN)
        {
            string retString = string.Empty;
            string url = $@"{this.webApiPort}MesApi/api/Product/GetBarcodeInfo";
            //List MESMAC = new List();
            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(resInfo.ToString())!; //JsonConvert.DeserializeObject(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 

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

你可能感兴趣的:(c#,json,java)