Using JsonConvert.DeserializeObject

Can anyone help me just deserialize a simple JSON response, I can't seem to find the answer anywere!
JSON Response: "{\"AuthResponse\":true,\"ContactID\":9999}"

I just want to be able to store the single line response to local variables.
Example:

var response = await httpClient.GetStringAsync(myURL);

//response returns "{\"AuthResponse\":true,\"ContactID\":9999}"

var bool AuthRespsone = "This is the part I need help with"

Best Answer

Answers

  • KevinMcCarthyKevinMcCarthy USMember ✭✭
    edited January 24

    @LandLu I did implement those changes and now I am getting:

    Newtonsoft.Json.JsonSerializationException: 'Error converting value "{"AuthResponse":true,"ContactID":9999}" to type 'USM.Model'. Path ''

    Yet if I hard code the string as in your example, it deserializes it fine! It's like Xamarin doesn't like my JSON!

    Thanks in advanced!

  • LandLuLandLu Member, Xamarin Team Xamurai

    Try to check the validation of your json string here:
    https://jsonlint.com/
    It seems the string is invalidated lacking symbols like "\".

  • KevinMcCarthyKevinMcCarthy USMember ✭✭

    It did return Valid when copying the response from Postman. I must be missing something.

  • KevinMcCarthyKevinMcCarthy USMember ✭✭
    edited January 25

    Thanks for the snippet, my issue dealt with casting the API back to mobile app. Postman was able to handle it, Xamarin was not. So I made some changes on the API level and your code worked great!

    Thanks

Sign In or Register to comment.