使用Newtonsoft.Json将字符串转换为对象列表



我收到一个来自API的响应,需要将其转换为Xamarin中的对象列表。我反序列化字符串的对象是:

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);
}
}

我试图将字符串反序列化为List<ReturnedImg>对象的代码是:

List<ReturnedImg> returnedImgs = JsonConvert.DeserializeObject<List<ReturnedImg>>(response);

,其中response是返回的字符串。我还试了这个:

ReturnedImg[] returnedImgs1 = JsonConvert.DeserializeObject<ReturnedImg[]>(response);

也不工作

不知何故,我的API响应在API和客户端之间的某个地方得到了双转义。

我从API接收到的字符串是:
""[{\"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\"}]""

当我用下面的字符串进行测试时,它确实正确地序列化了。

"[{"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"}]"

我得到的错误信息是:

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.'

与其他代码的唯一区别是错误消息中给出的类型是Label_Reader.ReturnedImg[]

当我查看API的CloudWatch日志时,方法响应没有被双重转义;下面是其中一条日志语句:

(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"}]"

dbc是正确的,说我的Json字符串是双重转义,因为在后台的东西。我的API调用一个lambda函数,该函数调用另一个lambda函数,并且我在两个返回语句中都使用了json.dumps()。从子函数的返回语句中删除json.dumps解决了这个问题,并防止了双转义的发生。

相关内容

  • 没有找到相关文章

最新更新