Search code examples
phpjsonstringify

Unable to json_decode an object I send with JSON.stringify()


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 ?


Solution

  • In-case someone cares about the solution:

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