最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - JSON Encodedecode doesn't work as it should - Stack Overflow

programmeradmin1浏览0评论

I am preparing and sending a JSON string from my PHP file to my Javascript function like this:

$json = array();
$json['slice'] = false;
$json['G500'] = false;
$json['KG1'] = false;   

$encoded = json_encode($json);
die($encoded);

However, in my JS function, if I do this, it is unable to decode the JSON object:

var d = req.responseText;                                           
var jsonObject = eval(d);

The only way, I can get it to eval the JSON object is by adding parentheses manually

jsonObject = eval("(" + d + ")");

I have the same problem going in reverse as well. Sending a JSON object to PHP and trying to decode it there fails. I believe I would need to remove the parentheses in my PHP script before attempting to decode.

Why is this happening? Is there something I can do to work around this inpatibility?

EDIT:

PHP to JS is now working if I use JSON.parse. I'm still having trouble the other way around.

This is how I'm sending the data to the PHP:

var JSONstring = 
{
    "Product": document.getElementById('item').value, 
    "Size": document.getElementById('size').value,
    "Quantity": document.getElementById('quantity').value                                                   
};

url = "maintainOrder.php?json=" + JSON.stringify(JSONstring);

req.open("GET", url, true);

However, the PHP script is unable to decode it.

$newItem = json_decode($_GET['json']);

array_push($_SESSION['order'],$newItem);

I am preparing and sending a JSON string from my PHP file to my Javascript function like this:

$json = array();
$json['slice'] = false;
$json['G500'] = false;
$json['KG1'] = false;   

$encoded = json_encode($json);
die($encoded);

However, in my JS function, if I do this, it is unable to decode the JSON object:

var d = req.responseText;                                           
var jsonObject = eval(d);

The only way, I can get it to eval the JSON object is by adding parentheses manually

jsonObject = eval("(" + d + ")");

I have the same problem going in reverse as well. Sending a JSON object to PHP and trying to decode it there fails. I believe I would need to remove the parentheses in my PHP script before attempting to decode.

Why is this happening? Is there something I can do to work around this inpatibility?

EDIT:

PHP to JS is now working if I use JSON.parse. I'm still having trouble the other way around.

This is how I'm sending the data to the PHP:

var JSONstring = 
{
    "Product": document.getElementById('item').value, 
    "Size": document.getElementById('size').value,
    "Quantity": document.getElementById('quantity').value                                                   
};

url = "maintainOrder.php?json=" + JSON.stringify(JSONstring);

req.open("GET", url, true);

However, the PHP script is unable to decode it.

$newItem = json_decode($_GET['json']);

array_push($_SESSION['order'],$newItem);
Share Improve this question edited Oct 27, 2011 at 2:32 Ayush asked Oct 27, 2011 at 2:15 AyushAyush 42.5k51 gold badges168 silver badges241 bronze badges 9
  • Demo: codepad/NBJnQqJR – Jared Farrish Commented Oct 27, 2011 at 2:17
  • does jsonObject=JSON.parse(req.responseText); work? – Robot Woods Commented Oct 27, 2011 at 2:19
  • The output you get is the exact one that I get as well. But, eval fails on it as it is. I need to add parentheses to get 'eval' to read it. – Ayush Commented Oct 27, 2011 at 2:20
  • @Robot: I'll try that now, but do I need to include a file to use JSON.parse? – Ayush Commented Oct 27, 2011 at 2:20
  • 1 @TomalakGeret'kal, you're confusing JavaScript and PHP. It is perfectly OK in PHP, they are called associative arrays in PHP, and is the same as hashes in Perl or Maps/HashMaps of Java. – stivlo Commented Oct 27, 2011 at 2:24
 |  Show 4 more ments

4 Answers 4

Reset to default 4

Your Javascript

eval has an issue with leading { characters, because of an ambiguity with block scope.

Using the parentheses to force the input to be parsed as an expression is a solution, but you should avoid eval entirely and use a proper JSON decoding function.


Your PHP

We'd need to see the data that you send to your PHP script to know why it won't parse. In general, as long as JSONLint accepts your JSON, so will PHP's json_decode. So give that a go.

For the php to javascript issue refer to the Tomalak Geret'kal answer.

For the javascript to php maybe I have the solution:

If you want an associative array in php then you have to pass assoc parameter as true into json_decode (default to false)

Example:

$array = json_decode($jsonString, true);

I was bitten a couple of times by this: by default json_decode try to create an object if it receive a javascript object (it make perfect sense if you think of) and you have to force it to render an associative array if you need this behaviour

I think you need to do some string processing, all you need to do is echo and see the exact format of your JSON string and make sure it conforms to the standard format, then use string processing both on the server and client sides to achieve the desired effect

Are you sure php is not adding slashes to the json text?, try saving the json text in a file in the server side to verify

发布评论

评论列表(0)

  1. 暂无评论