如何将C#/.NET 将json字符串格式数据转换成对象
答案:1 悬赏:50 手机版
解决时间 2021-11-12 02:22
- 提问者网友:骨子里的高雅
- 2021-11-11 08:21
如何将C#/.NET 将json字符串格式数据转换成对象
最佳答案
- 五星知识达人网友:掌灯师
- 2021-11-11 09:08
下个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(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 whh = JsonConvert.DeserializeObject
引用 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
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
- >(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; }
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯