2

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 incompatibility?

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);
Ayush
  • 41,754
  • 51
  • 164
  • 239

4 Answers4

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.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • what about the other way round, though? I am unable to decode a JSON object in my PHP. – Ayush Oct 27 '11 at 02:29
  • Added some more code. Also, trying JSONLint. Thanks for that link. Seems handy – Ayush Oct 27 '11 at 02:33
  • @xbonez: I don't want to see more Javascript. I want to see the PHP string that you're sending to PHP's `json_decode`. Anything in between could be mangling it, so let's find out! – Lightness Races in Orbit Oct 27 '11 at 02:34
  • added that as well. Also, the string being sent gets validated by JSONLint – Ayush Oct 27 '11 at 02:36
  • Got it working. Its decoding now, but not adding it to the `$_SESSION` array using `array_push`. That is a different issue, though. Thanks. – Ayush Oct 27 '11 at 02:43
  • @xbonez Are you shure that $_SESSION['order'] is an array? array_push should add every gibberish you pass to the array or spit an error – Eineki Oct 27 '11 at 03:07
3

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

Eineki
  • 14,773
  • 6
  • 50
  • 59
0

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

samuelagm
  • 131
  • 1
  • 4
0

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

samuelagm
  • 131
  • 1
  • 4