[英]deserialize nested objects in json and assign to c# list
I have a json which I am reading and trying to convert to list of PackageCategory我有一个 json 我正在阅读并尝试将其转换为 PackageCategory 列表
[
{
"PackageCategory": [
{
"Size": "xxx",
"Dimension": {
"Height": "000.0",
"Length": "1200.0",
"Breadth": "3300.0"
},
"Cost": "10.0m"
},
{
"Size": "yyy",
"Dimension": {
"Height": "1200.0",
"Length": "1400.0",
"Breadth": "5500.0"
},
"Cost": "790.5m"
},
{
"Size": "zzz",
"Dimension": {
"Height": "2550.0",
"Length": "4030.0",
"Breadth": "1600.0"
},
"Cost": "8.5m"
}
]
}
]
I want to deserialize it to C# list object List我想将其反序列化为 C# 列表 object 列表
public class PackageCategory
{
public string Size { get; set; }
public PackageSize Dimension { get; set; }
public decimal Cost { get; set; }
}
public class PackageSize
{
public double Height { get; }
public double Length { get; }
public double Breadth { get; }
}
I am using below code but getting null values to the list object listObjects我正在使用以下代码,但将 null 值添加到列表 object listObjects
using (StreamReader r = new StreamReader(@"\ListOfPackageCategory.json"))
{
string json = r.ReadToEnd();
var listObjects = JsonConvert.DeserializeObject<List<packageCategory>>(json)
}
Also tried with也试过了
JavaScriptSerializer ser = new JavaScriptSerializer();
var listObjects = ser.Deserialize<List<PackageCategory>>(json);
This also didnt work Please help to find the solution这也不起作用 请帮助找到解决方案
You need a Root
object to deserialize:您需要一个
Root
object 来反序列化:
JsonConvert.DeserializeObject<List<Root>>
: JsonConvert.DeserializeObject<List<Root>>
:
class Root
{
public List<PackageCategory> PackageCategory;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.