简体   繁体   中英

How to avoid backslashes in JSON returned from PHP?

I'm having some trouble with the code below. the console log returns:

"returningList{"view":0,"new":1,"random":1} result: "{\"view\":0,\"new\":1,\"random\":1}""

As you can see it's acquiring backslashes that make it no longer JSON, despite specifying datatype : 'json' .

The problem seems to be occurring where the result is read back in from PHP to javascript, because if I write $jsonArr to MySQL before echoing it then the SQL data shows the correct string (ie without the backslashes).

Please help because this apparent insanity is really winding me up. All I want to do is get my data back – In my actual code I don't normally return the JSON I passed in as data, but here it suffices to prove the point. I thought that the 'json' datatype would mean that it would accept echoed JSON without corrupting it.

Apologies for the following bluntness, but in response to similar questions I've seen people just say "it escapes the quotes", as if that that answers the question – in this case at least it seems that it doesn't: Obviously (AFAIK) that's where the backslashes are coming from, but the quotes are part of the JSON syntax (which it knows it's expecting), not part of the field content, so they clearly shouldn't be escaped. I need a solution please. Thanks.

window.newGot = listBuild({
    'view' : 0,
    'new' : 1,
    'random' : 1
});

function listBuild(options)
    //Fill the new list.
    return $.ajax({
        type : "POST",
        url : "./php/getList.php",
        cache : false,
        data : {
            options : JSON.stringify(options)
        },
        datatype : "json"
    }).then(function(jsonList) {
        console.log('returningList' + JSON.stringify(options) + ' result: ' + JSON.stringify(jsonList));
        return jsonList;
    });
}

Here's the php file contents:

<?php
$options = json_decode($_POST['options'], true);
$jsonArr = json_encode($options);
echo $jsonArr;

Whatever's generating the JSON is probably double-encoding, eg

$arr = array();
foreach($data as $foo) {
    $arr[] = json_encode($foo);
}
echo json_encode($arr);

JSON-encoding should be the very last step performed, just before the output to the client occurs.

eg

$foo = 'This is a string';
$foo_step1 = json_encode($foo); // "This is a string"
$foo_step2 = json_encode($foo_step1); // "\"This is a string\""

Depending on where the issue is, it might be because you are using json.stringify, have you tried parsing it as json instead?

Jquery has the following if you include the jquery library, otherwise the browser has one as well: $.parseJSON()

http://api.jquery.com/jquery.parsejson/

The other issues could be that you are double encoding it

Also run it through a validator, that is not valid code

http://jsonlint.com/

"returningList{"view":0,"new":1,"random":1} result: "{\\"view\\":0,\\"new\\":1,\\"random\\":1}""

This is what I would expect the json to look like

{
"returningList": {
    "view": 0,
    "new": 1,
    "random": 1
},
"result": {
    "view": 0,
    "new": 1,
    "random": 1
}

}

Apparently jsonList is the JSON string, not the decoded object that it represents. So when you call JSON.stringify() on it, you're re-encoding the string.

When you specify dataType: 'json' , jQuery is supposed to decode the response automatically. I know it does this when you specify the callback in the success: option, but it looks like it doesn't do it when you use .then() . Try using .done() instead.

With that said, I tried to reproduce your result at jsfiddle, and couldn't.

http://jsfiddle.net/barmar/53jthkyu/4/

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