C# Could not Cast or Convert System.String to Class object
It appears that the json you receive has been serialized twice - first from ApiResult
to string
, then to string
again:
"\"{\\"status\\":\\"0\\",\\"message\\":...
The first double-quote might be added by your debugger, but the second (the escaped \"
one) really appears to be part of the data you're processing. The error message also makes sense this way, it deserializes a string
and then attempts to cast it to an ApiResult
.
Try deserializing the data as a string and then deserializing its result to an ApiResult
, to be sure this is the case - and if so, the server code will need to be changed.
Below code worked for me along the lines of to C.Evenhuis answer,
var content = response.Content;
var jsonResult = JsonConvert.DeserializeObject(content).ToString();
var result= JsonConvert.DeserializeObject<Model>(jsonResult);
Here Content is similar to - "\"{\\"Id\\":\\"92209\\",\\"operatorId\\":100000,\\"Status\\":true, .....