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?
As an input parameter, our method receives a JSON string. After the deserialization, it returns a nullable Company with all the data. Inside this method, we create a company variable that is going to receive our deserialized object. Then we call the Deserialize method from the JsonSerializer static class and voilà.
JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object). If you serialize this result it will generate a text with the structure and the record returned.
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);
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With