JsonConvert does not deserialize object properly

后端 未结 1 1328
时光取名叫无心
时光取名叫无心 2021-01-26 10:38

I have this simply follow json and class that I want to DeserializeObject to object.

{\"UnsubscribeResponse\": {
   \"txId\": \"123\",
   \"result\": \"Success\"         


        
1条回答
  •  伪装坚强ぢ
    2021-01-26 11:32

    Add a new class:

    public class UnsubscribeResponsewrapper
    {
        public UnsubscribeResponse UnsubscribeResponse { get; set; }
    }
    

    and then rather than reading / deserialising as UnsubscribeResponse, use UnsubscribeResponseWrapper instead. This is necessary since your JSON is:

    {"UnsubscribeResponse": {
       "txId": "123",
       "result": "Success",
       "message": "OK"
    }}
    

    For your original code to work, the JSON would need to be something like:

    {
       "txId": "123",
       "result": "Success",
       "message": "OK"
    }
    

    You need the extra class since your JSON has the UnsubscribeResponse property. I suspect you are looking at your JSON and thinking this is a UnsubscribeResponse object and its properties. But that isn't actually what your JSON is. It is this is an object, with a UnsubscribeResponse property (and that property has a set of its own properties). As such, you need to either alter the JSON (as per my altered JSON) or alter the code (as per my class).

    0 讨论(0)
提交回复
热议问题