<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.simdjson.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.simdjson-decode.php',
    1 => 'simdjson_decode',
    2 => 'Decodes a JSON string',
  ),
  'up' => 
  array (
    0 => 'ref.simdjson.php',
    1 => 'Simdjson 函数',
  ),
  'prev' => 
  array (
    0 => 'ref.simdjson.php',
    1 => 'Simdjson 函数',
  ),
  'next' => 
  array (
    0 => 'function.simdjson-is-valid.php',
    1 => 'simdjson_is_valid',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/simdjson/functions/simdjson-decode.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.simdjson-decode" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">simdjson_decode</h1>
  <p class="verinfo">(PECL simdjson &gt;= 2.0.0)</p><p class="refpurpose"><span class="refname">simdjson_decode</span> &mdash; <span class="dc-title">Decodes a JSON string</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.simdjson-decode-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>simdjson_decode</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$json</code></span>, <span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$associative</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong></span></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$depth</code><span class="initializer"> = 512</span></span>): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>

  <p class="simpara">
   Takes a JSON encoded string and converts it into a PHP value.
   This uses a faster Simultaneous Instruction, Multiple Data implementation
   than <span class="function"><a href="function.json-decode.php" class="function">json_decode()</a></span> when it is supported by the computer architecture.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.simdjson-decode-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt><code class="parameter">json</code></dt>
    <dd>
     <span class="simpara">
      The <code class="parameter">json</code> <span class="type"><a href="language.types.string.php" class="type string">string</a></span> being decoded.
     </span>
     <span class="simpara">
      This function only works with UTF-8 encoded strings.
     </span>
     <span class="simpara">
      This function parses valid inputs which
      <span class="function"><a href="function.json-decode.php" class="function">json_decode()</a></span> can decode,
      provided that they are less than 4 GiB long.
     </span>
    </dd>
   
   
    <dt><code class="parameter">associative</code></dt>
    <dd>
     <span class="simpara">
      When <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, JSON objects will be returned as
      associative <span class="type"><a href="language.types.array.php" class="type array">array</a></span>s; when <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, JSON objects will be returned as <span class="type"><a href="language.types.object.php" class="type object">object</a></span>s.
     </span>
    </dd>
   
   
    <dt><code class="parameter">depth</code></dt>
    <dd>
     <span class="simpara">
      Maximum nesting depth of the structure being decoded.
      The value must be greater than <code class="literal">0</code>,
      and less than or equal to <code class="literal">2147483647</code>.

      Callers should use reasonably small values,
      because larger depths require more buffer space and will
      increase the recursion depth, unlike the current <span class="function"><a href="function.json-decode.php" class="function">json_decode()</a></span> implementation.
     </span>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.simdjson-decode-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="simpara">
   Returns the value encoded in <code class="parameter">json</code> in appropriate
   PHP type. Values <code class="literal">true</code>, <code class="literal">false</code> and
   <code class="literal">null</code> are returned as <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> and <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>
   respectively.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.simdjson-decode-errors">
  <h3 class="title">错误／异常</h3>
  <p class="simpara">
   If <code class="parameter">json</code> is invalid, a <span class="classname"><a href="class.simdjsonexception.php" class="classname">SimdJsonException</a></span> is thrown as of PECL simdjson 2.1.0,
   while previously, a <span class="classname"><a href="class.runtimeexception.php" class="classname">RuntimeException</a></span> was thrown.
  </p>
  <p class="simpara">
   If <code class="parameter">depth</code> is outside the allowed range,
   a <span class="classname"><a href="class.simdjsonvalueerror.php" class="classname">SimdJsonValueError</a></span> is thrown as of PECL simdjson 3.0.0,
   while previously, an error of level <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> was raised.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.simdjson-decode-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 <span class="function"><strong>simdjson_decode()</strong></span> examples</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$json </span><span style="color: #007700">= </span><span style="color: #DD0000">'{"a":1,"b":2,"c":3}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">simdjson_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">simdjson_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>以上示例会输出：</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">object(stdClass)#1 (3) {
  [&quot;a&quot;]=&gt;
  int(1)
  [&quot;b&quot;]=&gt;
  int(2)
  [&quot;c&quot;]=&gt;
  int(3)
}
array(3) {
  [&quot;a&quot;]=&gt;
  int(1)
  [&quot;b&quot;]=&gt;
  int(2)
  [&quot;c&quot;]=&gt;
  int(3)
}</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-2">
    <p><strong>示例 #2 Accessing invalid object properties</strong></p>
    <div class="example-contents"><p>
     Accessing elements within an object that contain characters not
     permitted under PHP&#039;s naming convention (e.g. the hyphen) can be
     accomplished by encapsulating the element name within braces and the apostrophe.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$json </span><span style="color: #007700">= </span><span style="color: #DD0000">'{"foo-bar": 12345}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$obj </span><span style="color: #007700">= </span><span style="color: #0000BB">simdjson_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">);<br />print </span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;{</span><span style="color: #DD0000">'foo-bar'</span><span style="color: #007700">}; </span><span style="color: #FF8000">// 12345<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <div class="example" id="example-3">
    <p><strong>示例 #3 common mistakes using <span class="function"><strong>simdjson_decode()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">// the following strings are valid JavaScript but not valid JSON<br /><br />// the name and value must be enclosed in double quotes<br />// single quotes are not valid<br /></span><span style="color: #0000BB">$bad_json </span><span style="color: #007700">= </span><span style="color: #DD0000">"{ 'bar': 'baz' }"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">simdjson_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$bad_json</span><span style="color: #007700">); </span><span style="color: #FF8000">// Throws SimdJsonException<br /><br />// the name must be enclosed in double quotes<br /></span><span style="color: #0000BB">$bad_json </span><span style="color: #007700">= </span><span style="color: #DD0000">'{ bar: "baz" }'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">simdjson_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$bad_json</span><span style="color: #007700">); </span><span style="color: #FF8000">// Throws SimdJsonException<br /><br />// trailing commas are not allowed<br /></span><span style="color: #0000BB">$bad_json </span><span style="color: #007700">= </span><span style="color: #DD0000">'{ bar: "baz", }'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">simdjson_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$bad_json</span><span style="color: #007700">); </span><span style="color: #FF8000">// Throws SimdJsonException<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <div class="example" id="example-4">
    <p><strong>示例 #4 <code class="parameter">depth</code> errors</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Encode some data with a maximum depth of 4<br />// (array -&gt; array -&gt; array -&gt; string)<br /></span><span style="color: #0000BB">$json </span><span style="color: #007700">= </span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(<br />    [<br />        </span><span style="color: #0000BB">1 </span><span style="color: #007700">=&gt; [<br />            </span><span style="color: #DD0000">'English' </span><span style="color: #007700">=&gt; [<br />                </span><span style="color: #DD0000">'One'</span><span style="color: #007700">,<br />                </span><span style="color: #DD0000">'January'<br />            </span><span style="color: #007700">],<br />            </span><span style="color: #DD0000">'French' </span><span style="color: #007700">=&gt; [<br />                </span><span style="color: #DD0000">'Une'</span><span style="color: #007700">,<br />                </span><span style="color: #DD0000">'Janvier'<br />            </span><span style="color: #007700">]<br />        ]<br />    ]<br />);<br /><br /></span><span style="color: #FF8000">// Show the errors for different depths.<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">simdjson_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">));<br />try {<br />    </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">simdjson_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">));<br />} catch (</span><span style="color: #0000BB">SimdJsonException $e</span><span style="color: #007700">) {<br />     echo </span><span style="color: #DD0000">"Caught: "</span><span style="color: #007700">, </span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>以上示例会输出：</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">array(1) {
  [1]=&gt;
  array(2) {
    [&quot;English&quot;]=&gt;
    array(2) {
      [0]=&gt;
      string(3) &quot;One&quot;
      [1]=&gt;
      string(7) &quot;January&quot;
    }
    [&quot;French&quot;]=&gt;
    array(2) {
      [0]=&gt;
      string(3) &quot;Une&quot;
      [1]=&gt;
      string(7) &quot;Janvier&quot;
    }
  }
}
Caught: The JSON document was too deep (too many nested objects and arrays)</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-5">
    <p><strong>示例 #5 <span class="function"><strong>simdjson_decode()</strong></span> of large integers</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$json </span><span style="color: #007700">= </span><span style="color: #DD0000">'{"number": 12345678901234567890}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">simdjson_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>以上示例会输出：</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">object(stdClass)#1 (1) {
  [&quot;number&quot;]=&gt;
  float(1.2345678901235E+19)
}</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.simdjson-decode-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    The JSON spec is not JavaScript, but a subset of JavaScript.
   </span>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    In the event of a failure to decode,
    a <span class="classname"><a href="class.simdjsonexception.php" class="classname">SimdJsonException</a></span> is thrown
    and <span class="methodname"><strong>SimdJsonException::getCode()</strong></span> and
    <span class="methodname"><strong>SimdJsonException::getMessage()</strong></span> can be used
    to determine the exact nature of the error.
   </span>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.simdjson-decode-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li><span class="function"><a href="function.json-encode.php" class="function" rel="rdfs-seeAlso">json_encode()</a> - 对变量进行 JSON 编码</span></li>
   <li><span class="function"><a href="function.json-decode.php" class="function" rel="rdfs-seeAlso">json_decode()</a> - 对 JSON 格式的字符串进行解码</span></li>
  </ul>
 </div>

</div><?php manual_footer($setup); ?>