您好,登錄后才能下訂單哦!
小編給大家分享一下php語(yǔ)言中json實(shí)現(xiàn)的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
php語(yǔ)言的json實(shí)現(xiàn)
由于開(kāi)發(fā)一個(gè)ajax file manager for web開(kāi)源項(xiàng)目,數(shù)據(jù)交換使用的json格式,后來(lái)發(fā)現(xiàn)在低版本的php上運(yùn)行會(huì)有問(wèn)題,仔細(xì)調(diào)試發(fā)現(xiàn)json_decode和json_encode無(wú)法正常工作,于是查閱資料,發(fā)現(xiàn)低版本的php沒(méi)有實(shí)現(xiàn)這兩個(gè)函數(shù),為了兼容性,我只好自己實(shí)現(xiàn)一個(gè)php版的json編碼解碼代碼,并保證和json2.js的一致,測(cè)試調(diào)試并通過(guò),現(xiàn)在將其公布出來(lái),供有相同需求的同學(xué)使用:
<?php /* * **************************************************************************** * $base: $ * * $Author: $ * Berlin Qin * * $History: base.js $ * Berlin Qin // created * * $contacted * webfmt@gmail.com * www.webfmt.com * * *************************************************************************** */ /* =========================================================================== * license * * 、Open Source Licenses * webfmt is distributed under the GPL, LGPL and MPL open source licenses. * This triple copyleft licensing model avoids incompatibility with other open source licenses. * These Open Source licenses are specially indicated for: * Integrating webfmt into Open Source software; * Personal and educational use of webfmt; * Integrating webfmt in commercial software, * taking care of satisfying the Open Source licenses terms, * while not able or interested on supporting webfmt and its development. * * 、Commercial License – fbis source Closed Distribution License - CDL * For many companies and products, Open Source licenses are not an option. * This is why the fbis source Closed Distribution License (CDL) has been introduced. * It is a non-copyleft license which gives companies complete freedom * when integrating webfmt into their products and web sites. * This license offers a very flexible way to integrate webfmt in your commercial application. * These are the main advantages it offers over an Open Source license: * Modifications and enhancements doesn't need to be released under an Open Source license; * There is no need to distribute any Open Source license terms alongside with your product * and no reference to it have to be done; * No references to webfmt have to be done in any file distributed with your product; * The source code of webfmt doesn't have to be distributed alongside with your product; * You can remove any file from webfmt when integrating it with your product. * The CDL is a lifetime license valid for all releases of webfmt published during * and before the year following its purchase. * It's valid for webfmt releases also. It includes year of personal e-mail support. * * ************************************************************************************************************************************************* */ function jsonDecode($json) { $result = array(); try { if (PHP_VERSION_ID > ) { $result = (array) json_decode($json); } else { $json = str_replace(array("\\\\", "\\\""), array("&#;", "&#;"), $json); $parts = preg_split("@(\"[^\"]*\")|([\[\]\{\},:])|\s@is", $json, -, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); foreach ($parts as $index => $part) { if (strlen($part) == ) { switch ($part) { case "[": case "{": $parts[$index] = "array("; break; case "]": case "}": $parts[$index] = ")"; break; case ":": $parts[$index] = "=>"; break; case ",": break; default: break; } } } $json = str_replace(array("&#;", "&#;", "$"), array("\\\\", "\\\"", "\\$"), implode("", $parts)); $result = eval("return $json;"); } } catch (Exception $e) { $result = array("error" => $e->getCode()); } return $result; } function valueTostr($val) { if (is_string($val)) { $val = str_replace('\"', "\\\"", $val); $val = str_replace("\\", "\\\\", $val); $val = str_replace("/", "\\/", $val); $val = str_replace("\t", "\\t", $val); $val = str_replace("\n", "\\n", $val); $val = str_replace("\r", "\\r", $val); $val = str_replace("\b", "\\b", $val); $val = str_replace("\f", "\\f", $val); return '"' . $val . '"'; } elseif (is_int($val)) return sprintf('%d', $val); elseif (is_float($val)) return sprintf('%F', $val); elseif (is_bool($val)) return ($val ? 'true' : 'false'); else return 'null'; } function jsonEncode($arr) { $result = "{}"; try { if (PHP_VERSION_ID > ) { $result = json_encode($arr); } else { $parts = array(); $is_list = false; if (!is_array($arr)) { $arr = (array) $arr; } $end = count($arr) - ; if (count($arr) > ) { if (is_numeric(key($arr))) { $result = "["; for ($i = ; $i < count($arr); $i++) { if (is_array($arr[$i])) { $result = $result . jsonEncode($arr[$i]); } else { $result = $result . valueTostr($arr[$i]); } if ($i != $end) { $result = $result . ","; } } $result = $result . "]"; } else { $result = "{"; $i = ; foreach ($arr as $key => $value) { $result = $result . '"' . $key . '":'; if (is_array($value)) { $result = $result . jsonEncode($value); } else { $result = $result . valueTostr($value); } if ($i != $end) { $result = $result . ","; } $i++; } $result = $result . "}"; } } else { $result = "[]"; } } } catch (Exception $e) { } return $result; } ?>
以上是“php語(yǔ)言中json實(shí)現(xiàn)的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。