How do I deserialize a tree of objects into a DTO?How do I use JSON.NET to deserialize into nested/recursive Dictionary and List?How do I calculate someone's age in C#?Deep cloning objectsHow do I enumerate an enum in C#?How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?How can I pretty-print JSON in a shell script?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How do I test for an empty JavaScript object?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Deserialize JSON into C# dynamic object?What is a NullReferenceException, and how do I fix it?
Can I take new (still in their boxes) PC parts in my checked in luggage?
My manager quit. Should I agree to defer wage increase to accommodate budget concerns?
Do I have advantage with Riposte when moving away from a flanked enemy and triggering an opportunity attack?
what should be done first, handling missing data or dealing with data types?
Why, even after his imprisonment, people keep calling Hannibal Lecter "Doctor"?
How to deal with a Homophobic PC
relating two diagrams in tikzcd
How to say "cheat sheet" in French
Is it impolite to ask for halal food when traveling to and in Thailand?
How 象【しょう】 ( ≈かたち、 すがた、ようす) and 象【ぞう】 (どうぶつ) got to be written with the same kanji?
Late 1970's and 6502 chip facilities for operating systems
Do we have any particular tonal center in mind when we are NOT listening music?
A famous scholar sent me an unpublished draft of hers. Then she died. I think her work should be published. What should I do?
Lost Update Understanding
Why solving a differentiated integral equation might eventually lead to erroneous solutions of the original problem?
How do pilots align the HUD with their eyeballs?
Can an integer optimization problem be convex?
My Project Manager does not accept carry-over in Scrum, Is that normal?
sed command works in terminal but not through bash_aliases or a bash script
Clear text passwords in Unix
Do we know the situation in Britain before Sealion (summer 1940)?
Why does (inf + 0j)*1 evaluate to inf + nanj?
Examples of "unsuccessful" theories with afterlives
List of 1000 most common words across all languages
How do I deserialize a tree of objects into a DTO?
How do I use JSON.NET to deserialize into nested/recursive Dictionary and List?How do I calculate someone's age in C#?Deep cloning objectsHow do I enumerate an enum in C#?How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?How can I pretty-print JSON in a shell script?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How do I test for an empty JavaScript object?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Deserialize JSON into C# dynamic object?What is a NullReferenceException, and how do I fix it?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am attempting to deserialize a tree of JSon objects I get back into a DTO. The tree can be up to 4 levels deep, and over 1200 nodes. The code is C# using Newtonsoft.Json for deserialization.
EDIT
The JSon looks like:
[
"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:
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?
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);
I have been experimenting with converting this code into a method to convert to MyDTO, but I keep running into compiler errors. I have attempted to simply convert the JValue casts with MyDTO and move on to the List with a List.
My goal is to call MyDTO obj = JsonHelp.Deserialize(jsonString)
and get the tree of MyDTO objects back. Is it possible to do what I am trying, or should I find a way to cast each object to MyDTO?
c# .net json json.net
add a comment
|
I am attempting to deserialize a tree of JSon objects I get back into a DTO. The tree can be up to 4 levels deep, and over 1200 nodes. The code is C# using Newtonsoft.Json for deserialization.
EDIT
The JSon looks like:
[
"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:
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?
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);
I have been experimenting with converting this code into a method to convert to MyDTO, but I keep running into compiler errors. I have attempted to simply convert the JValue casts with MyDTO and move on to the List with a List.
My goal is to call MyDTO obj = JsonHelp.Deserialize(jsonString)
and get the tree of MyDTO objects back. Is it possible to do what I am trying, or should I find a way to cast each object to MyDTO?
c# .net json json.net
1
json2csharp.com
– gsharp
Mar 28 at 18:07
I have edited it to make it more complete. Thanks.
– Blanthor
Mar 28 at 18:07
@gsharp. Thanks that shows that my scrubbing of the data didn't work. Fixing now.
– Blanthor
Mar 28 at 18:10
1
@Blanthor yeah something wrong in your json... but i always use that helper page to get a basic structure of the classes and then take it from there.
– gsharp
Mar 28 at 18:12
add a comment
|
I am attempting to deserialize a tree of JSon objects I get back into a DTO. The tree can be up to 4 levels deep, and over 1200 nodes. The code is C# using Newtonsoft.Json for deserialization.
EDIT
The JSon looks like:
[
"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:
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?
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);
I have been experimenting with converting this code into a method to convert to MyDTO, but I keep running into compiler errors. I have attempted to simply convert the JValue casts with MyDTO and move on to the List with a List.
My goal is to call MyDTO obj = JsonHelp.Deserialize(jsonString)
and get the tree of MyDTO objects back. Is it possible to do what I am trying, or should I find a way to cast each object to MyDTO?
c# .net json json.net
I am attempting to deserialize a tree of JSon objects I get back into a DTO. The tree can be up to 4 levels deep, and over 1200 nodes. The code is C# using Newtonsoft.Json for deserialization.
EDIT
The JSon looks like:
[
"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:
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?
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);
I have been experimenting with converting this code into a method to convert to MyDTO, but I keep running into compiler errors. I have attempted to simply convert the JValue casts with MyDTO and move on to the List with a List.
My goal is to call MyDTO obj = JsonHelp.Deserialize(jsonString)
and get the tree of MyDTO objects back. Is it possible to do what I am trying, or should I find a way to cast each object to MyDTO?
c# .net json json.net
c# .net json json.net
edited Mar 28 at 18:47
Blanthor
asked Mar 28 at 17:37
BlanthorBlanthor
1,5136 gold badges37 silver badges59 bronze badges
1,5136 gold badges37 silver badges59 bronze badges
1
json2csharp.com
– gsharp
Mar 28 at 18:07
I have edited it to make it more complete. Thanks.
– Blanthor
Mar 28 at 18:07
@gsharp. Thanks that shows that my scrubbing of the data didn't work. Fixing now.
– Blanthor
Mar 28 at 18:10
1
@Blanthor yeah something wrong in your json... but i always use that helper page to get a basic structure of the classes and then take it from there.
– gsharp
Mar 28 at 18:12
add a comment
|
1
json2csharp.com
– gsharp
Mar 28 at 18:07
I have edited it to make it more complete. Thanks.
– Blanthor
Mar 28 at 18:07
@gsharp. Thanks that shows that my scrubbing of the data didn't work. Fixing now.
– Blanthor
Mar 28 at 18:10
1
@Blanthor yeah something wrong in your json... but i always use that helper page to get a basic structure of the classes and then take it from there.
– gsharp
Mar 28 at 18:12
1
1
json2csharp.com
– gsharp
Mar 28 at 18:07
json2csharp.com
– gsharp
Mar 28 at 18:07
I have edited it to make it more complete. Thanks.
– Blanthor
Mar 28 at 18:07
I have edited it to make it more complete. Thanks.
– Blanthor
Mar 28 at 18:07
@gsharp. Thanks that shows that my scrubbing of the data didn't work. Fixing now.
– Blanthor
Mar 28 at 18:10
@gsharp. Thanks that shows that my scrubbing of the data didn't work. Fixing now.
– Blanthor
Mar 28 at 18:10
1
1
@Blanthor yeah something wrong in your json... but i always use that helper page to get a basic structure of the classes and then take it from there.
– gsharp
Mar 28 at 18:12
@Blanthor yeah something wrong in your json... but i always use that helper page to get a basic structure of the classes and then take it from there.
– gsharp
Mar 28 at 18:12
add a comment
|
1 Answer
1
active
oldest
votes
First your JSON is missing a brakcet ]
, then your model needs a nullable parentId
. Making a couple simple changes you can then you JsonConvert.DeserializeObject<IEnumerable<MyDTO>>(json)
.
So your JSON should look like this:
[
"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);
Both the model and code are compatible with your new valid JSON sample. Thank you for updating the OP.
– JSteward
Mar 28 at 18:30
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55403789%2fhow-do-i-deserialize-a-tree-of-objects-into-a-dto%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
First your JSON is missing a brakcet ]
, then your model needs a nullable parentId
. Making a couple simple changes you can then you JsonConvert.DeserializeObject<IEnumerable<MyDTO>>(json)
.
So your JSON should look like this:
[
"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);
Both the model and code are compatible with your new valid JSON sample. Thank you for updating the OP.
– JSteward
Mar 28 at 18:30
add a comment
|
First your JSON is missing a brakcet ]
, then your model needs a nullable parentId
. Making a couple simple changes you can then you JsonConvert.DeserializeObject<IEnumerable<MyDTO>>(json)
.
So your JSON should look like this:
[
"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);
Both the model and code are compatible with your new valid JSON sample. Thank you for updating the OP.
– JSteward
Mar 28 at 18:30
add a comment
|
First your JSON is missing a brakcet ]
, then your model needs a nullable parentId
. Making a couple simple changes you can then you JsonConvert.DeserializeObject<IEnumerable<MyDTO>>(json)
.
So your JSON should look like this:
[
"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);
First your JSON is missing a brakcet ]
, then your model needs a nullable parentId
. Making a couple simple changes you can then you JsonConvert.DeserializeObject<IEnumerable<MyDTO>>(json)
.
So your JSON should look like this:
[
"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);
answered Mar 28 at 18:24
JStewardJSteward
4,7502 gold badges10 silver badges20 bronze badges
4,7502 gold badges10 silver badges20 bronze badges
Both the model and code are compatible with your new valid JSON sample. Thank you for updating the OP.
– JSteward
Mar 28 at 18:30
add a comment
|
Both the model and code are compatible with your new valid JSON sample. Thank you for updating the OP.
– JSteward
Mar 28 at 18:30
Both the model and code are compatible with your new valid JSON sample. Thank you for updating the OP.
– JSteward
Mar 28 at 18:30
Both the model and code are compatible with your new valid JSON sample. Thank you for updating the OP.
– JSteward
Mar 28 at 18:30
add a comment
|
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55403789%2fhow-do-i-deserialize-a-tree-of-objects-into-a-dto%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
json2csharp.com
– gsharp
Mar 28 at 18:07
I have edited it to make it more complete. Thanks.
– Blanthor
Mar 28 at 18:07
@gsharp. Thanks that shows that my scrubbing of the data didn't work. Fixing now.
– Blanthor
Mar 28 at 18:10
1
@Blanthor yeah something wrong in your json... but i always use that helper page to get a basic structure of the classes and then take it from there.
– gsharp
Mar 28 at 18:12