OGeek|极客世界-中国程序员成长平台

标题: php - 如何解码json消息 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:55
标题: php - 如何解码json消息

我有以下 json 编码函数来显示 iphone 的推送通知..... 但我需要的只是将推送通知消息存储在数据库中...... 这样我就可以在使用 php 开发的网站中显示该消息.... 所以我需要解码这个json格式

private function _jsonEncode($array = false)
{
    //Using json_encode if exists
    if (function_exists('json_encode')) {
        return json_encode($array);
    }
    if (is_null($array))
        return 'null';
    if ($array === false)
        return 'false';
    if ($array === true)
        return 'true';
    if (is_scalar($array)) {
        if (is_float($array)) {
            return floatval(str_replace(",", ".", strval($array)));
        }
        if (is_string($array)) {
            static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
            return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $array) . '"';
        } else
            return $array;
    }
    $isList = true;
    for ($i = 0, reset($array); $i < count($array); $i++, next($array)) {
        if (key($array) !== $i) {
            $isList = false;
            break;
        }
    }
    $result = array();
    if ($isList) {
        foreach ($array as $v)
            $result[] = $this->_jsonEncode($v);
        return '[' . join(',', $result) . ']';
    } else {
        foreach ($array as $k => $v)
            $result[] = $this->_jsonEncode($k) . ':' . $this->_jsonEncode($v);
        return '{' . join(',', $result) . '}';
    }
}



Best Answer-推荐答案


如果我是你,我会使用标准的 PHP 函数:

json_encode http://php.net/manual/en/function.json-encode.php

json_decode http://php.net/manual/en/function.json-decode.php

关于php - 如何解码json消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18533191/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4