如何将C#/.NET 将json字符串格式数据转换成对象
展开全部
下个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; }
}
引用 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; }
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询