1

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.

4
  • 2
    What didnt work? Commented Jan 31, 2018 at 15:00
  • How to accesses to the values of the c# object? please help Commented Jan 31, 2018 at 15:02
  • The object returned from Welcome.FromJson() should have all the properties populated. Commented Jan 31, 2018 at 15:03
  • 2
    The funny thing is that OP's code works :) You wrote the method Welcome.FromJson, but in the last code snippet, you use System.Web.Script.Serialization.JavaScriptSerializer. Just use Welcome.FromJson, it works. Commented Jan 31, 2018 at 15:05

1 Answer 1

3

You should use

Welcome result = JsonConvert.DeserializeObject<Welcome>(myJson);

and then you can easily access it by using

Console.WriteLine(result.response.status.code);
Sign up to request clarification or add additional context in comments.

3 Comments

Might be worth mentioning why OP's code currently isnt working (i.e the last code lines in their example)
Thank you, I want to up-vote but I can't because I don't have enough reputation
@tgg you should still be able to "accept" the answer by clicking on the tick mark next to it, so it goes green. This gives the answerer 15 points and also makes it clear for future readers that there was a correct or best answer to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.