Deserialize JSON to list with unknown object name in C#

3941 views c#
3

I want to deserialize following JSON.
The problem is that the objects "ANDE" & "DAR" can change.
Means the objects are unknown and change depending on the JSON i wanna deserialize.

{"ANDE":
     {"chart":[
     {"date":"20180914","minute":"09:30"},{"date":"20180914","minute":"13:30"}]},
"DAR":
     {"chart":[
     {"date":"20180914","minute":"09:30"},{"date":"20180914","minute":"13:30"}]}}

How can i deserialize this kind of JSON with unknown objects in C#? Any help is highly appreciated.

answered question

If you known each different pattern use a json.net TypeConverter newtonsoft.com/json/help/html/CustomJsonConverter.htm

1 Answer

1

Then you should use a dynamic variable:

dynamic ReturnValue = JsonConvert.DeserializeObject(jsonstring);

note that as in dynamic objects, properties are determined after being assigned in runtime, so you will not get a drop down menu in design time, and also as its properties are unknown in design time, and property you test in design time even if its not correct, you wont get an error, and you will get the error in runtime when it is assigned.

posted this

Have an answer?

JD

Please login first before posting an answer.

Ads

Categories