如何将C#/.NET 将json字符串格式数据转换成对象

如题所述

第1个回答  推荐于2018-10-20
下个Newtonsoft.Json插件
引用 Newtonsoft.Json.dll

1、json字符串
string xxx = "{\"count\":\"1\",\"Product_Code\":\"14003949\",\"Product_Name\":\"聚丙烯树脂\",\"Product_Batch\":\"20140228D8103\",\"Certification_Code\":\"SCSH20140226-001-01\",\"Plate_Code\":\"L5E-89\",\"Grade\":\"合格品\",\"WarehouseIn_Num\":\"19120.0000000\",\"WarehouseIn_Weight\":\"478.000\",\"WarehouseIn_Confirm_Date\":\"2014-03-01\"}";

则直接转换为对象:

M_WarehouseInResult whh = JsonConvert.DeserializeObject<M_WarehouseInResult>(xxx);
2、如果为json数组(注意:最外是中括号)
string xxx = "[{\"count\":\"1\",\"Product_Code\":\"14003949\",\"Product_Name\":\"聚丙烯树脂\",\"Product_Batch\":\"20140228D8103\",\"Certification_Code\":\"SCSH20140226-001-01\",\"Plate_Code\":\"L5E-89\",\"Grade\":\"合格品\",\"WarehouseIn_Num\":\"19120.0000000\",\"WarehouseIn_Weight\":\"478.000\",\"WarehouseIn_Confirm_Date\":\"2014-03-01\"}]";

json数组转换为list

List<M_WarehouseInResult> whh = JsonConvert.DeserializeObject<List<M_WarehouseInResult>>(xxx);

附:
public class M_WarehouseInResult
{
public string count { get; set; }
public string Product_Code { get; set; }
public string Product_Name { get; set; }
public string Product_Batch { get; set; }
public string Certification_Code { get; set; }
public string Plate_Code { get; set; }
public string Grade { get; set; }
public string WarehouseIn_Num { get; set; }
public string WarehouseIn_Weight { get; set; }
public string WarehouseIn_Confirm_Date { get; set; }
}本回答被提问者和网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网