获取json格式内容
@param boolean $assoc 为true时返回数组，为false时返回对象
@param string $fromEncoding 请求返回数据的编码，如果不为空则进行编码转换
@param string $toEncoding 要转换到的编码，默认为UTF-8
@return mixed
543302
public function json($assoc = false, $fromEncoding = null, $toEncoding = 'UTF-8')
    {
        return json_decode($this->body($fromEncoding, $toEncoding), $assoc);
    }
624139
protected function jsonDecode(Field $f, $value, $assoc = true)
    {
        // constant supported only starting PHP 7.3
        if (!defined('JSON_THROW_ON_ERROR')) {
            define('JSON_THROW_ON_ERROR', 0);
        }

        $res = json_decode($value, $assoc, 512, JSON_THROW_ON_ERROR);
        if (JSON_THROW_ON_ERROR == 0 && json_last_error() !== JSON_ERROR_NONE) {
            throw new Exception([
                'There was error while decoding JSON',
                'code'  => json_last_error(),
                'error' => json_last_error_msg(),
            ]);
        }

        return $res;
    }
336586
public function json(array $json = [], $assoc = false)
    {
        $this->options['json']       = $json;
        $this->options['json_assoc'] = $assoc;
        return $this;
    }
75173
static function json_parse($json = '', $assoc = false, $depth = 512)
    {

        $parsed = \json_decode($json, $assoc, $depth);

        //if (JSON_ERROR_NONE !== json_last_error()) {
        //    throw new \RuntimeException('JSON Error: Unable to parse into JSON: ' . json_last_error());
        //}

        $error = json_last_error();

        switch ($error) {
            case JSON_ERROR_NONE:
                break;
            case JSON_ERROR_DEPTH:
                throw new InvalidArgumentException('JSON Error: Maximum stack depth exceeded');
                break;
            case JSON_ERROR_CTRL_CHAR:
                throw new InvalidArgumentException('JSON Error: Unexpected control character found');
                break;
            case JSON_ERROR_SYNTAX:
                throw new InvalidArgumentException('JSON Error: Syntax error, malformed JSON');
                break;
            default:
                throw new InvalidArgumentException('JSON Error: Unknown error');
                break;
        }

        // This is a courtesy by PHP JSON parser to parse "null" into null, but this is an error situation
        if (empty($parsed)) {
            throw new InvalidArgumentException('JSON Error: Result is empty after parsing');
        }

        return $parsed;

    }
1085673
public function jsonDecode($json, $assoc = false)
    {
        // if json_decode exists use that
        if (extension_loaded('json') && function_exists('json_decode')) {
            $decodedValue = json_decode($json, $assoc);
            if (!empty($decodedValue)) {
                return $decodedValue;
            }
        }

        try {
            return \Zend\Json\Decoder::decode(
                $json,
                $assoc
                    ? \Zend\Json\Json::TYPE_ARRAY
                    : \Zend\Json\Json::TYPE_OBJECT
            );
        } catch (Exception $exception) {
            // if a string is no valid json this call will throw Zend\Json\Exception\RuntimeException
            return null;
        }
    }
361349
public function callBack($appId, $fromClient, $to, $fromSerNum = null, $toSerNum = null, $type = 'json')
    {
        $url = self::BaseUrl . self::SoftVersion . '/Accounts/' . $this->accountSid . '/Calls/callBack?sig=' . $this->getSigParameter();
        if ($type === 'json') {
            $body_json = array('callback' => array(
                'appId'      => $appId,
                'fromClient' => $fromClient,
                'fromSerNum' => $fromSerNum,
                'to'         => $to,
                'toSerNum'   => $toSerNum,
            ));
            $body = json_encode($body_json);
        } elseif ($type === 'xml') {
            $body_xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
                        <callback>
                            <fromClient>' . $fromClient . '</clientNumber>
                            <fromSerNum>' . $fromSerNum . '</chargeType>
                            <to>' . $to . '</charge>
                            <toSerNum>' . $toSerNum . '</toSerNum>
                            <appId>' . $appId . '</appId>
                        </callback>';
            $body = trim($body_xml);
        } else {
            throw new Exception('只能json或xml，默认为json');
        }
        $data = $this->getResult($url, $body, $type, 'post');

        return $data;
    }
316665
public function jsonDecode($string, $assoc = false)
    {
        if (is_string($string)) {
            return json_decode($string, $assoc);
        }
        return null;
    }
661090
public static function convertEncoding( $str, $to_encoding = NULL, $from_encoding = NULL )
    {
        if ( is_string($str) && $to_encoding ){
            // エンコードあり
            return mb_convert_encoding($str,$to_encoding, $from_encoding);
        }
        // エンコード無し
        return $str;
    }
1013828
protected function _json_encode($arg)
    {
        /*
         * This function is very similar to json_encode, however it will traverse array
         * before encoding in search of jsExpressionable objects. Those would
         * be replaced with their jsRendering.
         */
        if (is_object($arg)) {
            if ($arg instanceof jsExpressionable) {
                $result = $arg->jsRender();

                return $result;
            } else {
                throw new Exception(['Not sure how to represent this object in JSON', 'obj' => $arg]);
            }
        } elseif (is_array($arg)) {
            $array = [];
            // is array associative? (hash)
            $assoc = $arg != array_values($arg);

            foreach ($arg as $key => $value) {
                $value = $this->_json_encode($value);
                $key = $this->_json_encode($key);
                if (!$assoc) {
                    $array[] = $value;
                } else {
                    $array[] = $key.':'.$value;
                }
            }

            if ($assoc) {
                $string = '{'.implode(',', $array).'}';
            } else {
                $string = '['.implode(',', $array).']';
            }
        } elseif (is_string($arg)) {
            $string = '"'.$this->_safe_js_string($arg).'"';
        } elseif (is_bool($arg)) {
            $string = json_encode($arg);
        } elseif (is_numeric($arg)) {
            $string = json_encode($arg);
        } elseif (is_null($arg)) {
            $string = json_encode($arg);
        } else {
            throw new Exception(['Unable to json_encode value - unknown type', 'arg' => var_export($arg, true)]);
        }

        return $string;
    }
475839
public function getData($json = true, $assoc = false)
    {
        $data = file_get_contents('php://input');
        if (!$json) {
            return $data;
        }
        return json_decode($data, $assoc);
    }
1175197
public static function decode(string $text, string $fromCharset): string
    {
        static $utf8Aliases = [
            'unicode-1-1-utf-8' => true,
            'utf8'              => true,
            'utf-8'             => true,
            'UTF8'              => true,
            'UTF-8'             => true,
        ];

        if (isset($utf8Aliases[$fromCharset])) {
            return $text;
        }

        $originalFromCharset  = $fromCharset;
        $lowercaseFromCharset = \strtolower($fromCharset);
        if (isset(self::$charsetAliases[$lowercaseFromCharset])) {
            $fromCharset = self::$charsetAliases[$lowercaseFromCharset];
        }

        \set_error_handler(static function () {});

        $iconvDecodedText = \iconv($fromCharset, 'UTF-8', $text);
        if (false === $iconvDecodedText) {
            $iconvDecodedText = \iconv($originalFromCharset, 'UTF-8', $text);
        }

        \restore_error_handler();

        if (false !== $iconvDecodedText) {
            return $iconvDecodedText;
        }

        $errorMessage = null;
        $errorNumber  = 0;
        \set_error_handler(static function ($nr, $message) use (&$errorMessage, &$errorNumber) {
            $errorMessage = $message;
            $errorNumber = $nr;
        });

        $decodedText = \mb_convert_encoding($text, 'UTF-8', $fromCharset);

        \restore_error_handler();

        if (null !== $errorMessage) {
            throw new UnsupportedCharsetException(\sprintf(
                'Unsupported charset "%s"%s: %s',
                $originalFromCharset,
                ($fromCharset !== $originalFromCharset) ? \sprintf(' (alias found: "%s")', $fromCharset) : '',
                $errorMessage
            ), $errorNumber);
        }

        return $decodedText;
    }
964748
public function convert($text, $toEncoding, $fromEncoding = null)
    {
        $fromEncoding = $fromEncoding ?: $this->defaultEncoding;

        return @iconv($fromEncoding, $toEncoding, $text);
    }
516694
public static function decode(string $json, bool $assoc = true)
    {
        $decoded = json_decode($json, $assoc);

        if ((unset) $decoded === $decoded) {
            throw new \InvalidArgumentException(sprintf(
                "Json decode failed (%s) with '%s' and data '%s'",
                json_last_error(),
                json_last_error_msg(),
                strlen($json) > 50 ? substr($json, 0, 150) . '...' : $json
            ));
        }

        return $decoded;
    }
1021212
public static function new(bool $assoc = false, int $options = 0, int $depth = 512): Json
    {
        return new self($assoc, $options, $depth);
    }
767953
public static function autoCharset($string, $from = 'gbk', $to = 'utf-8')
    {
        $from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;
        $to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;
        if (strtoupper($from) === strtoupper($to) || empty($string) || (is_scalar($string) && !is_string($string))) {
            //如果编码相同或者非字符串标量则不转换
            return $string;
        }
        if (is_string($string)) {
            if (function_exists('mb_convert_encoding')) {
                return mb_convert_encoding($string, $to, $from);
            } elseif (function_exists('iconv')) {
                return iconv($from, $to, $string);
            } else {
                return $string;
            }
        } elseif (is_array($string)) {
            foreach ($string as $key => $val) {
                $_key = self::autoCharset($key, $from, $to);
                $string[$_key] = self::autoCharset($val, $from, $to);
                if ($key != $_key)
                    unset($string[$key]);
            }
            
            return $string;
        } else {
            return $string;
        }
    }
983062
private function getJson($path, $zip, $file, $assoc = FALSE) {
    // Get stream
    $stream = $zip->getStream($file);
    if (!$stream) {
      // Breaking error, no need to continue validating.
      $this->h5pF->setErrorMessage($this->h5pF->t('Unable to read file from the package: %fileName', array('%fileName' => $file)), 'unable-to-read-package-file');
      $zip->close();
      unlink($path);
      return NULL;
    }

    // Read data
    $contents = '';
    while (!feof($stream)) {
      $contents .= fread($stream, 2);
    }

    // Decode the data
    $json = json_decode($contents, $assoc);
    if ($json === NULL) {
      // JSON cannot be decoded or the recursion limit has been reached.
      $this->h5pF->setErrorMessage($this->h5pF->t('Unable to parse JSON from the package: %fileName', array('%fileName' => $file)), 'unable-to-parse-package');
      return FALSE;
    }

    // All OK
    return $json;
  }
246910
private function decode($string, $fieldName, $assoc = false)
    {
        $jsonError = false;
        $result    = json_decode($string, $assoc);
        switch (json_last_error()) {
            case JSON_ERROR_NONE:
                break;
            case JSON_ERROR_DEPTH:
                $jsonError = 'Maximum stack depth exceeded';
                break;
            case JSON_ERROR_STATE_MISMATCH:
                $jsonError = 'Underflow or the modes mismatch';
                break;
            case JSON_ERROR_CTRL_CHAR:
                $jsonError = 'Unexpected control character found';
                break;
            case JSON_ERROR_SYNTAX:
                $jsonError = 'Syntax error, malformed JSON';
                break;
            case JSON_ERROR_UTF8:
                $jsonError = 'Malformed UTF-8 characters, possibly incorrectly encoded';
                break;
            default:
                $jsonError = 'Unknown error';
                break;
        }
        if ($jsonError) {
            throw new \Exception('JSON is invalid in field '.$fieldName.' JSON error: '.$jsonError);
        }

        return $result;
    }