{"id":125,"date":"2011-09-06T06:10:20","date_gmt":"2011-09-06T06:10:20","guid":{"rendered":"http:\/\/www.xiit.cn\/?p=125"},"modified":"2011-09-06T06:10:20","modified_gmt":"2011-09-06T06:10:20","slug":"php%e6%95%b0%e7%bb%84%e8%bd%ac%e6%88%90js-json%e6%a0%bc%e5%bc%8f%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/xiit.cn\/index.php\/2011\/09\/06\/php%e6%95%b0%e7%bb%84%e8%bd%ac%e6%88%90js-json%e6%a0%bc%e5%bc%8f%e6%95%b0%e6%8d%ae\/","title":{"rendered":"php\u6570\u7ec4\u8f6c\u6210js json\u683c\u5f0f\u6570\u636e"},"content":{"rendered":"<p>\u9879\u76ee\u4e2d\u8981\u7528\u5230JS\u8de8\u57df,\u6240\u4ee5\u5c31\u52a8\u6001\u52a0\u8f7dJS\u5185\u5bb9\u65b9\u6cd5\u76f4\u63a5\u4f20JSON\u6570\u636e\u663e\u793a\u9875\u9762<br \/>\n\u4f7f\u7528\u65b9\u6cd5: $json = json_encode_self($arr);<\/p>\n<p><code>function json_encode_self($a=false)<br \/>\n  {<br \/>\n    if (is_null($a)) return 'null';<!--more--><br \/>\n    if ($a === false) return 'false';<br \/>\n    if ($a === true) return 'true';<br \/>\n    if (is_scalar($a))<br \/>\n    {<br \/>\n      if (is_float($a))<br \/>\n      {<br \/>\n        \/\/ Always use \".\" for floats.<br \/>\n        return floatval(str_replace(\",\", \".\", strval($a)));<br \/>\n      }<\/p>\n<p>      if (is_string($a))<br \/>\n      {<br \/>\n        static $jsonReplaces = array(array(\"\\\", \"\/\", \"n\", \"t\", \"r\", \"b\", \"f\", '\"'), array('\\\\', '\\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));<br \/>\n        return '\"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '\"';<br \/>\n      }<br \/>\n      else<br \/>\n        return $a;<br \/>\n    }<br \/>\n    $isList = true;<br \/>\n    for ($i = 0, reset($a); $i < count($a); $i++, next($a))\n    {\n      if (key($a) !== $i)\n      {\n        $isList = false;\n        break;\n      }\n    }\n    $result = array();\n    if ($isList)\n    {\n      foreach ($a as $v) $result[] = json_encode_self($v);\n      return '[' . join(',', $result) . ']';\n    }\n    else\n    {\n      foreach ($a as $k => $v) $result[] = json_encode_self($k).':'.json_encode_self($v);<br \/>\n      return '{' . join(',', $result) . '}';<br \/>\n    }<br \/>\n  }<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9879\u76ee\u4e2d\u8981\u7528\u5230JS\u8de8\u57df,\u6240\u4ee5\u5c31\u52a8\u6001\u52a0\u8f7dJS\u5185\u5bb9\u65b9\u6cd5\u76f4\u63a5\u4f20JSON\u6570\u636e\u663e\u793a\u9875\u9762 \u4f7f\u7528\u65b9\u6cd5: $json = jso [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,15],"tags":[],"class_list":["post-125","post","type-post","status-publish","format-standard","hentry","category-js","category-php-2"],"_links":{"self":[{"href":"https:\/\/xiit.cn\/index.php\/wp-json\/wp\/v2\/posts\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xiit.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xiit.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xiit.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xiit.cn\/index.php\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":0,"href":"https:\/\/xiit.cn\/index.php\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"wp:attachment":[{"href":"https:\/\/xiit.cn\/index.php\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xiit.cn\/index.php\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xiit.cn\/index.php\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}