Deserializing a child into an object

I've tried some examples on here but am tearing my hair out.

I do a query and it returns JSON, inside the JSON are lots of hashes, eg.

{ "gjwiegjeigj": { ....}, "gjeeigjwoeigj": {...} ... }

I want to loop through each of these, and deserialize the contents into an object.

I've created the object, myObject which has all the fields, but I am stuck on deserialising.

I can deserialise straight from the base object using JsonConvert.DeserializeObject but I can't do that, I need to loop through and do that to the children.

I want an array of my custom objects with all the fields taken from the Json as a result of this, I don't care about the title of each one (the garbage hash).

Any ideas? I know I can loop through, which gives me lots of JTokens but that's where I get stuck.


Edit: Reading your question again, you mention both knowing and not knowing all the fields. It sounds like you really don't know exactly what fields the JSON string will contain.

For cases like this, I suggest you use dynamic -- this is where it shines. If you do know all the field names, your class should be deserializing without any issue.

What have you tried? Show us some real code, and real exceptions or problems.

To deserialize into a list of dynamic objects is simple:

dynamic toReturn = JsonConvert.DeserializeObject<List<dynamic>>(rawJson);

You should get back a list of dynamic objects. You can poke it for the fields you want:


So I figured it out, basically to get from a collection JTokens which is what I get when I iterate through .Children() on my JSON object, I can either cast it to a JProperty and do .Name to get the name or .Value to get the value, or I can deserialize directly into an object, essentially like this:

MyObject record = (MyObject)JsonConvert.DeserializeObject(myRow.Children().First().ToString(), typeof(MyObject), settings);

Then I don't know need to know the name of the property I am deserializing.

Need Your Help

Unable to click form button using tampermonkey

javascript jquery tampermonkey

I am working on a tampermonkey/greasemonkey script that will click a button on a webpage when the page is loaded. Here is the code for the form.

get value of a property by its path in

c# reflection

here a solution is given to get value of a property of a class by supplying its name . now I wonder how I can do the same in this condition :