0

I have the following class

 public class CallbackResultsJson
    {
    public class CallbackResults
    {

        public string Status { get; set; }
        public string Message { get; set; }
        public string Data { get; set; }
        public string Log { get; set; }
        public string StatusText { get; set; }
        public string TransactionToken { get; set; }
    }
}

I am trying to use Json.Net to Deserialize requestbody but I am always getting a null for status,data. any ideas why ?

var requestbody =@"
{
    "CallbackResults":
    {
        "TransactionToken":"b65524-qwe",
        "Status":0,
        "Message":"Publish Application to QaLevel Operation Completed",
        "Data":[],
        "Log":["sucess"
    },
    "RequestNumber":"REQ1234"
}"

var TransactionResult = JsonConvert.DeserializeObject<CallbackResultsJson.CallbackResults>(requestBody);
3
  • In your JSON, Status is an integer, not a string, Data is an array, not a string, Log is a badly formed array (no end bracket). You should get an exception with this JSON, and even if you fix the array for Log, you will get an exception because of the mismatch of types. Commented Jun 24, 2019 at 6:55
  • json2csharp.com Commented Jun 24, 2019 at 6:56
  • Please post actual code, this code won't even compile because of the badly formed string (you use " inside the string but should use ""), you're missing a semicolon, and the class names you're using in the deserialization call doesn't match the example class above. You've basically retyped the problem here and made additional errors. Commented Jun 24, 2019 at 6:56

3 Answers 3

2

Just a little bit of change should be done. Your class:

public class CallbackResultsJson
{
    public CallbackResultsClass CallbackResults { get; set; }
    public string RequestNumber { get; set; }

    public class CallbackResultsClass
    {
        public int Status { get; set; }
        public string Message { get; set; }
        public string[] Data { get; set; }
        public string Log { get; set; }
        public string TransactionToken { get; set; }
    }
}

Your data:

var requestbody = @"
        {
            ""CallbackResults"":
            {
                ""TransactionToken"":""b65524-qwe"",
                ""Status"":0,
                ""Message"":""Publish Application to QaLevel Operation Completed"",
                ""Data"":[""Data1"", ""Data2""],
                ""Log"":""sucess""
            },
            ""RequestNumber"":""REQ1234""
        }";


        var result = JsonConvert.DeserializeObject<CallbackResultsJson>(requestbody);

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

Your class needs to have a property of the sub-type,

public class CallbackResultsJson
{
    public CallBackResults CallbackResults { get; set; }

    public string RequestNumber { get; set; )

    public class CallbackResults
    {

        public string Status { get; set; }
        public string Message { get; set; }
        public string Data { get; set; }
        public string Log { get; set; }
        public string StatusText { get; set; }
        public string TransactionToken { get; set; }
    }
}

var result = JsonConvert.DeserializeObject<CallbackResultsJson>(requestBody);

Comments

0

I have used json2csharp.com to convert requestbody to c# classes and it generated these classes.it worked

 public class CallbackResults
    {
        public string TransactionToken { get; set; }
        public int Status { get; set; }
        public string Message { get; set; }
        public List<string> Data { get; set; }
        public List<string> Log { get; set; }
    }

    public class CallbackResultsRootObj
    {
        public CallbackResults VdiCallbackResults { get; set; }
        public string RequestNumber { get; set; }
    }
  var vdiTransactionResult = JsonConvert.DeserializeObject<CallbackResultsRootObj>(requestBody);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.