How do I deserialize a complex JSON object in C# .NET?
Solution 1
I am using like this in my code and it's working fine
below is a piece of code which you need to write
using System.Web.Script.Serialization;
JavaScriptSerializer oJS = new JavaScriptSerializer();
RootObject oRootObject = new RootObject();
oRootObject = oJS.Deserialize<RootObject>(Your JSon String);
Solution 2
Should just be this:
var jobject = JsonConvert.DeserializeObject<RootObject>(jsonstring);
You can paste the json string to here: http://json2csharp.com/ to check your classes are correct.
Solution 3
If you use C# 2010 or newer, you can use dynamic type:
dynamic json = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonstring);
Then you can access attributes and arrays in dynamic object using dot notation:
string nemo = json.response[0].images[0].report.nemo;
Solution 4
First install newtonsoft.json
package to Visual Studio
using NuGet Package Manager
then add the following code:
ClassName ObjectName = JsonConvert.DeserializeObject < ClassName > (jsonObject);
Solution 5
I had a scenario, and this one helped me
JObject
objParserd = JObject
.Parse(jsonString);
JObject
arrayObject1 = (JObject
)objParserd["d"];
D
myOutput= JsonConvert
.DeserializeObject<D>
(arrayObject1.ToString());
Assaf Zigdon
Updated on July 25, 2022Comments
-
Assaf Zigdon almost 2 years
I have a JSON string and I need some help to deserialize it.
Nothing worked for me... This is the JSON:
{ "response": [{ "loopa": "81ED1A646S894309CA1746FD6B57E5BB46EC18D1FAff", "drupa": "D4492C3CCE7D6F839B2BASD2F08577F89A27B4ff", "images": [{ "report": { "nemo": "unknown" }, "status": "rock", "id": "7e6ffe36e-8789e-4c235-87044-56378f08m30df", "market": 1 }, { "report": { "nemo": "unknown" }, "status": "rock", "id": "e50e99df3-59563-45673-afj79e-e3f47504sb55e2", "market": 1 } ] }] }
I have an example of the classes, but I don't have to use those classes. I don't mind using some other classes.
These are the classes:
public class Report { public string nemo { get; set; } } public class Image { public Report report { get; set; } public string status { get; set; } public string id { get; set; } public int market { get; set; } } public class Response { public string loopa { get; set; } public string drupa{ get; set; } public Image[] images { get; set; } } public class RootObject { public Response[] response { get; set; } }
I want to mention that I have Newtonsoft.Json already, so I can use some functions from there.
How can I do this?
-
Assaf Zigdon about 11 yearsi tried this, but i am only getting "The name 'does not exist in the current context" when entering to the quick view
-
stevepkr84 about 11 yearsDid you try classes as per the website I linked? I see they are different in that they use List<T> rather than arrays in the RootObject and Response classes. Not sure if it would make a difference.
-
Assaf Zigdon about 11 yearsyes, i changed it for a test, but the original was from this site
-
Allen Linatoc almost 9 yearsCould have been a good idea, but not working for me: One or more types required to compile a dynamic expression cannot be found. Are you missing a reference
-
jmbmage over 8 yearsFYI: this is in System.Web.Extensions.dll
-
Mich over 5 yearsWhere can I find the RootObject class??!
-
Peter Mortensen about 5 yearsBut will that handle the irregular types in this particular JSON (in the question). Have you actually tried to run it?
-
Peter Mortensen about 5 yearsBut will that handle the irregular types in this particular JSON (in the question). Have you actually tried to run it?
-
davidthegrey about 5 yearsIt handles missing json properties quite well (it will automatically convert to nullable types when it recognises properties not repeated in the arrays of objects). Of course having a c# converter class would not make sense if the json was completely irregular/randomic. In those situations, using dynamic is the way to go. In complex situations, like the project I am working on, I had to make some minor manual adjustments, eg converting some integer types to decimal (if the sample you submit does not contain fractional figures), manually adding nullables, ...
-
Ann Kilzer over 4 yearsWelcome to Stack Overflow. Perhaps some clearer formatting could help improve your answer.
-
AntonioR over 4 years@Mich, RootObject class was provided as part of the model classes at the end of the code of the question.
-
Spencer Sullivan about 4 years@Javal Patel - Powerful stuff! The "auto-name" mapping saves writing a lot of mapping code. Thank you for sharing!