简体   繁体   English

JSON.parse提供了意外的令牌错误

[英]JSON.parse gives unexpected token error

I receive this data when I call my api 调用api时会收到此数据

[{"DispatchNo":"xxxxx","DispatchDate":"xxxxxxx","Complete":"xxx","CustomerID":"xxxxx","Name":"xxxxxx","Contact":"","Phone":"xxxxx","ShipPhone":"xxxx","PurchaseOrder":"xxxx","OrderLoads":"5","OrderQty":"125","FreightUnitID":"x 

my controoller.js code where it indicates failure is: 我的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);

I get error SyntaxError: Unexpected token C in JSON at position 898 at JSON.parse () I believe it is failing at orderFromApex = JSON.parse(orderFromApex). 我收到错误SyntaxError:JSON中的意外令牌C,位于JSON.parse()的位置898,我认为它在orderFromApex = JSON.parse(orderFromApex)处失败。 I am also console logging orderFromApex which I have posted above. 我也是在上面发布的控制台记录orderFromApex。 What is going on? 到底是怎么回事? is there a problem with my JSON response? 我的JSON响应有问题吗?

Your API seems to not escape characters correctly. 您的API似乎无法正确转义字符。 " for example appears unescaped inside strings closing them before they should be closed. " ,例如在未关闭的字符串中出现未转义的字符,然后应将其关闭。

{"example": "Hello "World"!"} should instead be {"example": "Hello \\"World\\"!"} . {"example": "Hello "World"!"}应该改为{"example": "Hello \\"World\\"!"}

Trying to parse the first example will throw SyntaxError: Unexpected token W in JSON at position 20 . 尝试解析第一个示例将SyntaxError: Unexpected token W in JSON at position 20抛出SyntaxError: Unexpected token W in JSON at position 20 That's because the parser will look at the " before World and think Oh, the string is already over, what's this weird W doing after it? . 这是因为解析器将在World之前查看"并认为哦,字符串已经结束了,这之后奇怪的W在做什么?”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM