0

I have a script that posts a large object with AJAX after JSON.stringify().

When I am trying to decode it in PHP using json_decode($object, true); it won't be decoded.

My object looks something like:

var object = [
    {field_name:"Date & Time", some_other_value:"somevalue1"}
]

I am fairly sure it has something to do with the Date & Time. I am pretty sure that when I build the object, the value I insert into field_name is Date & Time

In PHP I've tried:

json_decode($object, true);
json_decode(utf8_decode($object))// with true as well.
json_decode(htmlentities($object, ENT_QUOTES, "UTF-8");

None seem to work.

UPDATE: I used alert() on the stringify and this is what i get:

"fields":{"29411502":{"id":29411502,"name":"Date & Time","functionName":""}}

Anyone with an idea ?

Stephan Weinhold
  • 1,623
  • 1
  • 28
  • 37
Neta Meta
  • 4,001
  • 9
  • 42
  • 67
  • Your sample isn't technically valid JSON and wouldn't/shouldn't be generated by `JSON.stringify()`. (It's missing quotes around they property names.) Can you show us what the *actual* output of `JSON.stringify()` is? – svidgen Nov 19 '13 at 21:07
  • yea will get the output 1 sec – Neta Meta Nov 19 '13 at 21:10
  • I've updated the post the with string i get. Should have included it before. but why the down vote ? – Neta Meta Nov 19 '13 at 21:20
  • Your update is not syntactically valid JSON. At a minimum, it's missing a closing }. – bishop Nov 19 '13 at 21:22
  • yaps - it's a much bigger object i cut it wrong but its not the issue – Neta Meta Nov 19 '13 at 21:26
  • Sounds like when you post, you are not using the text/javascript content type. &amp's never get converted for me. – Rahly Nov 19 '13 at 22:29

5 Answers5

1

In-case someone cares about the solution:

I had to us encodeURIcomponenet() on the stringified object.

Saurav Rastogi
  • 9,575
  • 3
  • 29
  • 41
Neta Meta
  • 4,001
  • 9
  • 42
  • 67
0

If you remove the & characters is the PHP script suddenly able to decode the object correctly?

Is so, might you need double encoding on the ampersand character? Is it possible it's getting decoded prior to the remainder of your message and causing a break in the parse?

Lorin S.
  • 754
  • 8
  • 29
0

This:

<?php

var_dump(
    json_decode(
        '[
            {"field_name":"Date &amp; Time", "some_other_value":"somevalue1"},
            {"field_name":"Date &amp; Time", "some_other_value":"somevalue2"},
            {"field_name":"Date &amp; Time", "some_other_value":"somevalue3"}
        ]'
    ),
    json_last_error(),
    PHP_VERSION
);

?>

Results in:

array(3) {
  [0]=>
  object(stdClass)#1 (2) {
    ["field_name"]=>
    string(15) "Date &amp; Time"
    ["some_other_value"]=>
    string(10) "somevalue1"
  }
  [1]=>
  object(stdClass)#2 (2) {
    ["field_name"]=>
    string(15) "Date &amp; Time"
    ["some_other_value"]=>
    string(10) "somevalue2"
  }
  [2]=>
  object(stdClass)#3 (2) {
    ["field_name"]=>
    string(15) "Date &amp; Time"
    ["some_other_value"]=>
    string(10) "somevalue3"
  }
}
int(0)
string(17) "5.3.15-pl0-gentoo"

Seems right to me...

bishop
  • 37,830
  • 11
  • 104
  • 139
0

Works for me

test.html

<html>
<body>
<script src="js/jquery/jquery-2.0.3.js"></script>
<button id="bob">Click ME</button>
<script>
(function($){
  $('#bob').click(function() {
    $.ajax({
      method: "POST",
      url: "test.php",
      data: JSON.stringify([{"this":"is &amp; test"}]),
      contentType: "text/javascript"
    }).done(function(a) {
      alert(a);
    });
  });
})(jQuery);
</script>
</body>
</html>

test.php

<?php

$data = file_get_contents("php://input");

var_dump($data);

var_dump(json_decode($data, true));

Produces a nice popup of

string(26) "[{"this":"is &amp; test"}]"
array(1) {
  [0] =>
  array(1) {
    'this' =>
    string(13) "is &amp; test"
  }
}
Rahly
  • 1,462
  • 13
  • 16
-2

place single quotes around parameter and value

var object = [
{'field_name':'Date &amp; Time', 'some_other_value':'somevalue1'},
....
lordkain
  • 3,061
  • 1
  • 13
  • 18
  • Nope that's not it.. its something with the & – Neta Meta Nov 19 '13 at 21:09
  • Should be double quotes, not single quotes, per JSON spec. See eg: http://stackoverflow.com/questions/4201441/is-there-any-practical-reason-to-use-quoted-strings-for-json-keys – bishop Nov 19 '13 at 21:23