简体   繁体   中英

JSON.parse gives unexpected token error

I receive this data when I call my 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:

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). I am also console logging orderFromApex which I have posted above. What is going on? is there a problem with my JSON response?

Your API seems to not escape characters correctly. " for example appears unescaped inside strings closing them before they should be closed.

{"example": "Hello "World"!"} should instead be {"example": "Hello \\"World\\"!"} .

Trying to parse the first example will throw 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? .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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