I want to convert this Json string to a c# object, I tried to learn from some tutorials but it didn't work, I am trying to convert this json string:
string myJson = "{\"response\": {\"status\": {\"code\": 1002,\"label\": \"UPLOAD_SUCCESS\",\"message\": \"The file was uploaded successfully.\"},\"md5\": \"da855ff838250a05692f14e\",\"file_name\": \"MyFile.docx\",\"file_type\": \"docx\",\"features\": [ \"te\" ],\"te\": {\"images\": [{\"report\": {\"verdict\": \"unknown\"},\"status\": \"not_found\",\"id\": \"5e5de275-a103-4f67-b532918fa59\",\"revision\": 1},{\"report\": {\"verdict\": \"unknown\"},\"status\": \"not_found\",\"id\": \"7e6fe36e-889e-4c25-8704-5637830df\",\"revision\": 1}],\"status\": {\"code\": 1001,\"label\": \"FOUND\",\"message\": \"The requested data has been found.\"}}}}";
I created this classs:
public partial class Welcome
{
[JsonProperty("response")]
public Response response { get; set; }
}
public partial class Response
{
[JsonProperty("status")]
public Status status { get; set; }
[JsonProperty("md5")]
public string md5 { get; set; }
[JsonProperty("file_name")]
public string fileName { get; set; }
[JsonProperty("file_type")]
public string file_type { get; set; }
[JsonProperty("features")]
public string[] features { get; set; }
[JsonProperty("te")]
public Te te { get; set; }
}
public partial class Status
{
[JsonProperty("code")]
public long code { get; set; }
[JsonProperty("label")]
public string label { get; set; }
[JsonProperty("message")]
public string message { get; set; }
}
public partial class Te
{
[JsonProperty("images")]
public Image[] images { get; set; }
[JsonProperty("status")]
public Status status { get; set; }
}
public partial class Image
{
[JsonProperty("report")]
public Report report { get; set; }
[JsonProperty("status")]
public string status { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("revision")]
public long Revision { get; set; }
}
public partial class Report
{
[JsonProperty("verdict")]
public string verdict { get; set; }
}
public partial class Welcome
{
public static Welcome FromJson(string json) => JsonConvert.DeserializeObject<Welcome>(json, Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this Welcome self) => JsonConvert.SerializeObject(self, Converter.Settings);
}
public class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
};
}
Finally I tried to accesses to the values of the object:
Welcome result = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Welcome>(json);
Console.WriteLine(result);
Can you show me how to convert this Json string to C# objects? I looked for a lot of tutorials but I didn't succeeded for a while so maybe you can help me to save some hours, please.
Welcome.FromJson()should have all the properties populated.