我打电话给api时收到这些数据
[{"DispatchNo":"xxxxx","DispatchDate":"xxxxxxx","Complete":"xxx","CustomerID":"xxxxx","Name":"xxxxxx","Contact":"","Phone":"xxxxx","ShipPhone":"xxxx","PurchaseOrder":"xxxx","OrderLoads":"5","OrderQty":"125","FreightUnitID":"x
我的controoller.js代码表示失败:
var jsonString = result.data.replace(/\\/g, "\\");
var orderFromApex = JSON.parse(jsonString);
orderFromApex = orderFromApex.substring(0);
console.log(orderFromApex);
//orderFromApex += '"';
orderFromApex = JSON.parse(orderFromApex);
console.log(orderFromApex);
我得到错误SyntaxError:位于898的JSON中的意外标记C. 在JSON.parse()我相信它在orderFromApex = JSON.parse(orderFromApex)失败了。我也是我在上面发布的控制台记录orderFromApex。到底是怎么回事?我的JSON响应有问题吗?
答案 0 :(得分:5)
您的API似乎无法正确转义字符。例如,"
在关闭它们之前会在关闭它们的字符串中显示为未转义。
{"example": "Hello "World"!"}
应改为{"example": "Hello \"World\"!"}
。
尝试解析第一个示例将抛出SyntaxError: Unexpected token W in JSON at position 20
。这是因为解析器会在"
之前查看World
并认为哦,字符串已经结束了,这个奇怪的W
做了什么?它?