<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.json.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'ja',
  ),
  'this' => 
  array (
    0 => 'function.json-decode.php',
    1 => 'json_decode',
    2 => 'JSON 文字列をデコードする',
  ),
  'up' => 
  array (
    0 => 'ref.json.php',
    1 => 'JSON 関数',
  ),
  'prev' => 
  array (
    0 => 'ref.json.php',
    1 => 'JSON 関数',
  ),
  'next' => 
  array (
    0 => 'function.json-encode.php',
    1 => 'json_encode',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'ja',
    'path' => 'reference/json/functions/json-decode.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.json-decode" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">json_decode</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.2.0, PHP 7, PHP 8, PECL json &gt;= 1.2.0)</p><p class="refpurpose"><span class="refname">json_decode</span> &mdash; <span class="dc-title">JSON 文字列をデコードする</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.json-decode-description">
  <h3 class="title">説明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>json_decode</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$json</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></span> <code class="parameter">$associative</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<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>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span><br>): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
   JSON エンコードされた文字列を受け取り、それを
   PHP の値に変換します。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.json-decode-parameters">
  <h3 class="title">パラメータ</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">json</code></dt>
     <dd>
      <p class="para">
       デコード対象となる <code class="parameter">json</code> 文字列。
      </p>
      <p class="para">
       この関数は UTF-8 でエンコードされた文字列でのみ動作します。
      </p>
      
 <blockquote class="note"><p><strong class="note">注意</strong>: 
  <p class="para">
   PHP の実装は、
   <a href="https://datatracker.ietf.org/doc/html/rfc7159" class="link external">&raquo;&nbsp;RFC 7159</a> の JSON のスーパーセットです。
  </p>
 </p></blockquote>

     </dd>
    
    
     <dt><code class="parameter">associative</code></dt>
     <dd>
      <p class="para">
       <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> の場合、返されるオブジェクトは <span class="type"><a href="language.types.object.php" class="type object">object</a></span> になります。
       <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> の場合、返されるオブジェクトは <code class="parameter">flags</code> に
       <strong><code><a href="json.constants.php#constant.json-object-as-array">JSON_OBJECT_AS_ARRAY</a></code></strong> が設定されているかどうかによって、
       連想配列形式か <span class="type"><a href="language.types.object.php" class="type object">object</a></span> かが変化します。
      </p>
     </dd>
    
    
     <dt><code class="parameter">depth</code></dt>
     <dd>
      <p class="para">
       デコードするデータのネストの深さの最大値。
       この値は <code class="literal">0</code> より大きく、
       <code class="literal">2147483647</code> 以下でなければいけません。
      </p>
     </dd>
    
    
     <dt><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       <strong><code><a href="json.constants.php#constant.json-bigint-as-string">JSON_BIGINT_AS_STRING</a></code></strong>,
       <strong><code><a href="json.constants.php#constant.json-invalid-utf8-ignore">JSON_INVALID_UTF8_IGNORE</a></code></strong>,
       <strong><code><a href="json.constants.php#constant.json-invalid-utf8-substitute">JSON_INVALID_UTF8_SUBSTITUTE</a></code></strong>,
       <strong><code><a href="json.constants.php#constant.json-object-as-array">JSON_OBJECT_AS_ARRAY</a></code></strong>,
       <strong><code><a href="json.constants.php#constant.json-throw-on-error">JSON_THROW_ON_ERROR</a></code></strong>.
       からなるビットマスク。
       各定数の意味については
       <a href="json.constants.php" class="link">JSON 定数のページ</a>
       に説明があります。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.json-decode-returnvalues">
  <h3 class="title">戻り値</h3>
  <p class="para">
   <code class="parameter">json</code> でエンコードされたデータを、適切な PHP の型として返します。
   クォートされていない <code class="literal">true</code>、<code class="literal">false</code> および
   <code class="literal">null</code> はそれぞれ <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>
   そして <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> として返されます。
   <code class="parameter">json</code> のデコードに失敗したり
   エンコードされたデータがネストの最大値を超えているなどの場合、<strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> を返します。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.json-decode-errors">
  <h3 class="title">エラー / 例外</h3>
  <p class="para">
   <code class="parameter">depth</code> が範囲外の値の場合、
   PHP 8.0.0 以降では
   <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> がスローされます。
   それより前のバージョンでは、
   <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> レベルの警告が発生していました。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.json-decode-changelog">
  <h3 class="title">変更履歴</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>バージョン</th>
       <th>説明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.3.0</td>
       <td>
        <code class="parameter">flags</code> パラメータに
        <strong><code><a href="json.constants.php#constant.json-throw-on-error">JSON_THROW_ON_ERROR</a></code></strong>
        が追加されました。
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        <code class="parameter">associative</code> は nullable になりました。
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        <code class="parameter">flags</code> パラメータに
        <strong><code><a href="json.constants.php#constant.json-invalid-utf8-ignore">JSON_INVALID_UTF8_IGNORE</a></code></strong> および
        <strong><code><a href="json.constants.php#constant.json-invalid-utf8-substitute">JSON_INVALID_UTF8_SUBSTITUTE</a></code></strong> が追加されました。
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        空の JSONキー  (&quot;&quot;) は、
        <code class="literal">_empty_</code> というキーではなく、
        空のオブジェクトプロパティにエンコードされるようになりました。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.json-decode-examples">
  <h3 class="title">例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>例1 <span class="function"><strong>json_decode()</strong></span> の例</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,"d":4,"e":5}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_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">json_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 (5) {
    [&quot;a&quot;] =&gt; int(1)
    [&quot;b&quot;] =&gt; int(2)
    [&quot;c&quot;] =&gt; int(3)
    [&quot;d&quot;] =&gt; int(4)
    [&quot;e&quot;] =&gt; int(5)
}

array(5) {
    [&quot;a&quot;] =&gt; int(1)
    [&quot;b&quot;] =&gt; int(2)
    [&quot;c&quot;] =&gt; int(3)
    [&quot;d&quot;] =&gt; int(4)
    [&quot;e&quot;] =&gt; int(5)
}</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-2">
    <p><strong>例2 無効なオブジェクトプロパティへのアクセス</strong></p>
    <div class="example-contents"><p>
     オブジェクトの中にある、
     PHP の命名規約では使えない文字 (ハイフンなど) を含む要素にアクセスするには、
     要素名を波括弧とアポストロフィで囲みます。
    </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">json_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 <span class="function"><strong>json_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">// 以下の文字列は JavaScript としては有効ですが JSON としては無効です<br /><br />// 名前と値はダブルクォートで囲む必要があります。<br />// シングルクォートは使えません<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">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$bad_json</span><span style="color: #007700">); </span><span style="color: #FF8000">// null<br /><br />// 名前をダブルクォートで囲まなければなりません<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">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$bad_json</span><span style="color: #007700">); </span><span style="color: #FF8000">// null<br /><br />// 最後にカンマをつけてはいけません<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">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$bad_json</span><span style="color: #007700">); </span><span style="color: #FF8000">// null<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> エラー</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">// データをエンコードします。ネストの深さは4です (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 />    array(<br />        </span><span style="color: #0000BB">1 </span><span style="color: #007700">=&gt; array(<br />            </span><span style="color: #DD0000">'English' </span><span style="color: #007700">=&gt; array(<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; array(<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">// さまざまな深さのエラーを表示します<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_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 />echo </span><span style="color: #DD0000">'Last error: '</span><span style="color: #007700">, </span><span style="color: #0000BB">json_last_error_msg</span><span style="color: #007700">(), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_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 />echo </span><span style="color: #DD0000">'Last error: '</span><span style="color: #007700">, </span><span style="color: #0000BB">json_last_error_msg</span><span style="color: #007700">(), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<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;
    }
  }
}
Last error: No error

NULL
Last error: Maximum stack depth exceeded</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-5">
    <p><strong>例5 <span class="function"><strong>json_decode()</strong></span> で大きな整数値を扱う例</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">json_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">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">, </span><span style="color: #0000BB">512</span><span style="color: #007700">, </span><span style="color: #0000BB">JSON_BIGINT_AS_STRING</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)
}
object(stdClass)#1 (1) {
  [&quot;number&quot;]=&gt;
  string(20) &quot;12345678901234567890&quot;
}</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.json-decode-notes">
  <h3 class="title">注意</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    JSON の仕様は JavaScript そのものではなく、JavaScript のサブセットです。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    デコードに失敗した場合は、<span class="function"><a href="function.json-last-error.php" class="function">json_last_error()</a></span>
    を使用すればエラーの正確な状態を知ることができます。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.json-decode-seealso">
  <h3 class="title">参考</h3>
  <p class="para">
   <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-last-error.php" class="function" rel="rdfs-seeAlso">json_last_error()</a> - 直近に発生したエラーを返す</span></li>
    <li><span class="function"><a href="function.json-last-error-msg.php" class="function" rel="rdfs-seeAlso">json_last_error_msg()</a> - 直近の json_validate()、json_encode()、json_decode() の呼び出しのエラー文字列を返す</span></li>
   </ul>
  </p>
 </div>

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