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