[英]How do I deserialize a tree of objects into a DTO?
I am attempting to deserialize a tree of JSon objects I get back into a DTO. 我试图反序列化返回到DTO的JSon对象树。 The tree can be up to 4 levels deep, and over 1200 nodes.
该树最多可以有4个级别的深度,并超过1200个节点。 The code is C# using Newtonsoft.Json for deserialization.
该代码是使用Newtonsoft.Json进行反序列化的C#。
EDIT 编辑
The JSon looks like: JSon看起来像:
[
{
"id": 1095,
"name": "Item1-1",
"children": [
{
"id": 1097,
"name": "Item2-2",
"children": [
{
"id": 18,
"name": "Item3-3",
"children": [
{
"id": 19,
"name": "Item4-4",
"children": [],
"level": 4,
"parentId": 18
},
{
"id": 20,
"name": "Item5-4",
"children": [],
"level": 4,
"parentId": 18
}
],
"level": 3,
"parentId": 1097
}
],
"level": 2,
"parentId": 1095
}
],
"level": 1,
"parentId": null
}
]
My DTO is similar to this: 我的DTO与此类似:
public class MyDTO
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("children")]
public MyDTO[] Children { get; set; }
[JsonProperty("level")]
public int Level { get; set; }
[JsonProperty("parentId")]
public int ParentId { get; set; }
public MyDTO()
{
}
}
I like Brian Rogers' solution in this link, which deserializes the json into objects: How do I use JSON.NET to deserialize into nested/recursive Dictionary and List? 我喜欢此链接中的Brian Rogers解决方案,该解决方案将json反序列化为对象: 如何使用JSON.NET反序列化为嵌套/递归Dictionary和List?
public static class JsonHelper
{
public static object Deserialize(string json)
{
return ToObject(JToken.Parse(json));
}
private static object ToObject(JToken token)
{
switch (token.Type)
{
case JTokenType.Object:
return token.Children<JProperty>()
.ToDictionary(prop => prop.Name,
prop => ToObject(prop.Value));
case JTokenType.Array:
return token.Select(ToObject).ToList();
default:
return ((JValue)token).Value;
}
}
}
I call the JSonHelper with object obj = JsonHelper.Deserialize(jsonString);
我用
object obj = JsonHelper.Deserialize(jsonString);
调用object obj = JsonHelper.Deserialize(jsonString);
I have been experimenting with converting this code into a method to convert to MyDTO, but I keep running into compiler errors. 我一直在尝试将此代码转换为可转换为MyDTO的方法,但是我一直遇到编译器错误。 I have attempted to simply convert the JValue casts with MyDTO and move on to the List with a List.
我试图简单地使用MyDTO转换JValue强制转换,然后转到带有列表的列表。
My goal is to call MyDTO obj = JsonHelp.Deserialize(jsonString)
and get the tree of MyDTO objects back. 我的目标是调用
MyDTO obj = JsonHelp.Deserialize(jsonString)
并获取MyDTO对象的树。 Is it possible to do what I am trying, or should I find a way to cast each object to MyDTO? 是否可以做我想做的事情,还是应该找到一种将每个对象投射到MyDTO的方法?
First your JSON is missing a brakcet ]
, then your model needs a nullable parentId
. 首先,您的JSON缺少brakcet
]
,然后您的模型需要可为null的parentId
。 Making a couple simple changes you can then you JsonConvert.DeserializeObject<IEnumerable<MyDTO>>(json)
. 进行一些简单的更改,便可以使用
JsonConvert.DeserializeObject<IEnumerable<MyDTO>>(json)
。
So your JSON should look like this: 因此,您的JSON应该如下所示:
[
{
"id": 1095,
"name": "Item1-1",
"children": [
{
"id": 1097,
"name": "Item2-2",
"children": [
{
"id": 18,
"name": "Item3-3",
"children": [
{
"id": 19,
"name": "Item4-4",
"children": [],
"level": 4,
"parentId": 18
},
{
"id": 20,
"name": "Item5-4",
"children": [],
"level": 4,
"parentId": 18
}
],
"level": 2,
"parentId": 1095
}],
}],
"level": 1,
"parentId": null
}
]
And can be deserialized with this model: 可以使用以下模型反序列化:
public class MyDTO
{
public int Id { get; set; }
public string Name { get; set; }
public MyDTO[] Children { get; set; }
public int Level { get; set; }
public int? ParentId { get; set; }
}
Using this code: 使用此代码:
var dto = JsonConvert.DeserializeObject<IEnumerable<MyDTO>>(json);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.