哈希空间 Ctrl + F 进行搜索
首页 php手册中文版 CSS中文手册 哈希文档 Markdown在线工具

PHP simdjson 组件

simdjson_decode

(PECL simdjson >= 2.0.0)

simdjson_decodeDecodes a JSON string

说明

simdjson_decode(string $json, bool $associative = false, int $depth = 512): mixed

Takes a JSON encoded string and converts it into a PHP value. This uses a faster Simultaneous Instruction, Multiple Data implementation than json_decode() when it is supported by the computer architecture.

参数

json

The json string being decoded.

This function only works with UTF-8 encoded strings.

This function parses valid inputs which json_decode() can decode, provided that they are less than 4 GiB long.

associative

When true, JSON objects will be returned as associative arrays; when false, JSON objects will be returned as objects.

depth

Maximum nesting depth of the structure being decoded. The value must be greater than 0, and less than or equal to 2147483647. Callers should use reasonably small values, because larger depths require more buffer space and will increase the recursion depth, unlike the current json_decode() implementation.

返回值

Returns the value encoded in json in appropriate PHP type. Values true, false and null are returned as true, false and null respectively.

错误/异常

If json is invalid, a SimdJsonException is thrown as of PECL simdjson 2.1.0, while previously, a RuntimeException was thrown.

If depth is outside the allowed range, a SimdJsonValueError is thrown as of PECL simdjson 3.0.0, while previously, an error of level E_WARNING was raised.

范例

示例 #1 simdjson_decode() examples

<?php
$json
= '{"a":1,"b":2,"c":3}';

var_dump(simdjson_decode($json));
var_dump(simdjson_decode($json, true));

?>

以上例程会输出:

object(stdClass)#1 (3) {
  ["a"]=>
  int(1)
  ["b"]=>
  int(2)
  ["c"]=>
  int(3)
}
array(3) {
  ["a"]=>
  int(1)
  ["b"]=>
  int(2)
  ["c"]=>
  int(3)
}

示例 #2 Accessing invalid object properties

Accessing elements within an object that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.

<?php

$json
= '{"foo-bar": 12345}';

$obj = simdjson_decode($json);
print
$obj->{'foo-bar'}; // 12345

?>

示例 #3 common mistakes using simdjson_decode()

<?php

// the following strings are valid JavaScript but not valid JSON

// the name and value must be enclosed in double quotes
// single quotes are not valid
$bad_json = "{ 'bar': 'baz' }";
simdjson_decode($bad_json); // Throws SimdJsonException

// the name must be enclosed in double quotes
$bad_json = '{ bar: "baz" }';
simdjson_decode($bad_json); // Throws SimdJsonException

// trailing commas are not allowed
$bad_json = '{ bar: "baz", }';
simdjson_decode($bad_json); // Throws SimdJsonException

?>

示例 #4 depth errors

<?php
// Encode some data with a maximum depth of 4
// (array -> array -> array -> string)
$json = json_encode(
[
1 => [
'English' => [
'One',
'January'
],
'French' => [
'Une',
'Janvier'
]
]
]
);

// Show the errors for different depths.
var_dump(simdjson_decode($json, true, 4));
try {
var_dump(simdjson_decode($json, true, 3));
} catch (
SimdJsonException $e) {
echo
"Caught: ", $e->getMessage(), "\n";
}
?>

以上例程会输出:

array(1) {
  [1]=>
  array(2) {
    ["English"]=>
    array(2) {
      [0]=>
      string(3) "One"
      [1]=>
      string(7) "January"
    }
    ["French"]=>
    array(2) {
      [0]=>
      string(3) "Une"
      [1]=>
      string(7) "Janvier"
    }
  }
}
Caught: The JSON document was too deep (too many nested objects and arrays)

示例 #5 simdjson_decode() of large integers

<?php
$json
= '{"number": 12345678901234567890}';

var_dump(simdjson_decode($json));

?>

以上例程会输出:

object(stdClass)#1 (1) {
  ["number"]=>
  float(1.2345678901235E+19)
}

注释

注意:

The JSON spec is not JavaScript, but a subset of JavaScript.

注意:

In the event of a failure to decode, a SimdJsonException is thrown and SimdJsonException::getCode() and SimdJsonException::getMessage() can be used to determine the exact nature of the error.

参见

打开 哈希空间 微信小程序中查看更佳