|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2008-07-22 14:03 UTC] jani@php.net
[2008-07-22 14:04 UTC] jani@php.net
[2008-07-22 15:06 UTC] coyote4til7 at gmail dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 30 03:00:01 2025 UTC |
Description: ------------ When you encode a proper javascript object as json and then use the json_decode function, it fails. The keys in Javascript objects are _not_ supposed to be quoted. If you're going to store a json-encoded object (say in a database) and then reuse it later in javascript, quoting the keys so that php's json_decode function works means that you're counting on the browser's javascript to support something that is not supported by the javascript specification. Reproduce code: --------------- // In javascript, we have an object: // var obj = { parent_id:-1,label:"label" }; // // In javascript, the keys for objects are _not_ quoted by // single or double quotes. So, if we're going to properly encode // this to later reuse it in javascript, we get this sent to php: $json = '[{parent_id:-1,label:"label"}]'; // Let's try to convert it: var_dump(json_decode($json, true)); Expected result: ---------------- { ["parent_id"]=> int(-1) ["label"]=> string(5) "label" } Actual result: -------------- NULL