Error parsing JSON with escaped quotes

I get the following json object when I call the URL from a browser that I do not expect in it.

"{\"data\":[], \"SkipToken\":\"\", \"top\":\"\"}"

However, when I tried to call it in javascript, it gave me error Parsing Json message

dspservice.callService(URL, "GET", "", function (data) {
    var dataList = JSON.parse(data);
)};

This code worked before I had no idea why it suddenly stopped working and threw me an error.

+4
source share
3 answers

Since there is nothing wrong with the JSON string you gave us, the only other explanation is that datapassing to your function is something other than what you specified.

To test this hypothesis, run the following code:

dspservice.callService(URL, "GET", "", handler(data));

function handler(data) {
    var goodData = "{\"data\":[], \"SkipToken\":\"\", \"top\":\"\"}";
    alert(goodData);                         // display the correct JSON string
    var goodDataList = JSON.parse(goodData); // parse good string (should work)
    alert(data);                             // display string in question
    var dataList = JSON.parse(data);         // try to parse it (should fail)
}

goodData JSON , data , .

handler, goodData . . , , JSON, , , .

+1

, JSON ?

"{\"data\":[], \"SkipToken\":\"\", \"top\":\"\"}"

data :

'"{\"data\":[], \"SkipToken\":\"\", \"top\":\"\"}"'

data - .

JSON:

{"data":[], "SkipToken":"", "top":""}
0

, JSON ( ):

{\"data\":[], \"SkipToken\":\"\", \"top\":\"\"}

JSON. JSON, , . JSON . ( , .)

JSON :

{"data":[], "SkipToken":"", "top":""}

, .

, , , , , , , :

var x = "{\"data\":[], \"SkipToken\":\"\", \"top\":\"\"}";

JavaScript-, escapes JSON,

{"data":[], "SkipToken":"", "top":""}

, , JSON.parse . escaping Javascript , , .

JSON-, . , , ( ) ( ). .

, , JSON.parse. ,

data.replace(/\\"/g, '"')

var dataList = JSON.parse(data.replace(/\\"/g, '"')

Additional configuration may be required depending on how the guy servers avoid quotes inside strings; Do they send, \"\\"\"or perhaps \"\\\"\"?

I can’t explain why this code that worked suddenly stopped working. My best guess is a server-side change that began to avoid double quotes.

0
source

All Articles