I receive a response from an API and need to convert it to a list of objects in my Xamarin.Forms app. The object that I am deserializing the string into is:
public class ReturnedImg
{
public string Type { get; set; }
public string Data { get; set; }
public double MinX { get; set; }
public double MaxX { get; set; }
public double MinY { get; set; }
public double MaxY { get; set; }
public ReturnedImg(string typ, string outp, string ymax, string ymin, string xmax, string xmin)
{
Type = typ;
Data = outp;
MinX = Convert.ToDouble(xmin);
MaxX = Convert.ToDouble(xmax);
MinY = Convert.ToDouble(ymin);
MaxY = Convert.ToDouble(ymax);
}
}
The code I attempt to use to deserialize the string into a List<ReturnedImg>
object is:
List<ReturnedImg> returnedImgs = JsonConvert.DeserializeObject<List<ReturnedImg>>(response);
where response is the returned string. I also tried this:
ReturnedImg[] returnedImgs1 = JsonConvert.DeserializeObject<ReturnedImg[]>(response);
which also didn't work.
Somehow, my API response gets double escaped somewhere between the API and the client.
The string I receive from the API is:
"\"[{\\\"typ\\\": \\\"barcode\\\", \\\"outp\\\": \\\"0882658000546\\\", \\\"ymax\\\": \\\"0.8211805555555556\\\", \\\"ymin\\\": \\\"0.7199900793650794\\\", \\\"xmax\\\": \\\"0.8296957671957672\\\", \\\"xmin\\\": \\\"0.3419312169312169\\\"}, {\\\"typ\\\": \\\"barcode\\\", \\\"outp\\\": \\\"A-0020-Z\\\", \\\"ymax\\\": \\\"0.39360119047619047\\\", \\\"ymin\\\": \\\"0.3881448412698413\\\", \\\"xmax\\\": \\\"0.7027116402116402\\\", \\\"xmin\\\": \\\"0.2919973544973545\\\"}, {\\\"typ\\\": \\\"barcode\\\", \\\"outp\\\": \\\"A-0010-Z\\\", \\\"ymax\\\": \\\"0.2881944444444444\\\", \\\"ymin\\\": \\\"0.20833333333333334\\\", \\\"xmax\\\": \\\"0.6924603174603174\\\", \\\"xmin\\\": \\\"0.2959656084656085\\\"}]\""
When I tested with the string below it did serialize correctly.
"[{\"typ\": \"barcode\", \"outp\": \"0882658000546\", \"ymax\": \"0.8343253968253969\", \"ymin\": \"0.8234126984126984\", \"xmax\": \"0.7235449735449735\", \"xmin\": \"0.22023809523809523\"}, {\"typ\": \"barcode\", \"outp\": \"A-0020-Z\", \"ymax\": \"0.48313492063492064\", \"ymin\": \"0.3916170634920635\", \"xmax\": \"0.6084656084656085\", \"xmin\": \"0.17427248677248677\"}, {\"typ\": \"barcode\", \"outp\": \"A-0010-Z\", \"ymax\": \"0.2934027777777778\", \"ymin\": \"0.21329365079365079\", \"xmax\": \"0.5949074074074074\", \"xmin\": \"0.18882275132275134\"}]"
The error message I'm getting is:
Newtonsoft.Json.JsonSerializationException: 'Error converting value "[{"typ": "barcode", "outp": "0882658000546", "ymax": "0.8343253968253969", "ymin": "0.8234126984126984", "xmax": "0.7235449735449735", "xmin": "0.22023809523809523"}, {"typ": "barcode", "outp": "A-0020-Z", "ymax": "0.48313492063492064", "ymin": "0.3916170634920635", "xmax": "0.6084656084656085", "xmin": "0.17427248677248677"}, {"typ": "barcode", "outp": "A-0010-Z", "ymax": "0.2934027777777778", "ymin": "0.21329365079365079", "xmax": "0.5949074074074074", "xmin": "0.18882275132275134"}]" to type 'System.Collections.Generic.List`1[Label_Reader.ReturnedImg]'. Path '', line 1, position 373.'
The only difference with the other code is that the type given in the error message was Label_Reader.ReturnedImg[]
.
When I looked at the CloudWatch logs for the API, however, the method response was not double escaped; here is one of the log statements:
(2722b67c-8991-40d8-a7d5-e6f9dfc4477d) Method response body after transformations: "[{\"typ\": \"barcode\", \"outp\": \"0882658000546\", \"ymax\": \"0.763640873015873\", \"ymin\": \"0.5935019841269841\", \"xmax\": \"0.8412698412698413\", \"xmin\": \"0.28075396825396826\"}, {\"typ\": \"barcode\", \"outp\": \"A-0020-Z\", \"ymax\": \"0.4419642857142857\", \"ymin\": \"0.3506944444444444\", \"xmax\": \"0.728505291005291\", \"xmin\": \"0.25892857142857145\"}, {\"typ\": \"barcode\", \"outp\": \"A-0010-Z\", \"ymax\": \"0.2544642857142857\", \"ymin\": \"0.18725198412698413\", \"xmax\": \"0.7106481481481481\", \"xmin\": \"0.29133597883597884\"}]"