mirror of
https://github.com/php/doc-pl.git
synced 2026-03-24 15:12:16 +01:00
365 lines
9.6 KiB
XML
365 lines
9.6 KiB
XML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!-- EN-Revision: 9f2e30a00afda6d6b6a3e19b13956150c2eaf2c1 Maintainer: grzesiek Status: ready -->
|
|
<!-- $Revision$ -->
|
|
<!-- CREDITS: sobak, joeaccord -->
|
|
<refentry xml:id="function.json-decode" xmlns="http://docbook.org/ns/docbook">
|
|
<refnamediv>
|
|
<refname>json_decode</refname>
|
|
<refpurpose>Dekoduje łańcuch JSON</refpurpose>
|
|
</refnamediv>
|
|
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<type>mixed</type><methodname>json_decode</methodname>
|
|
<methodparam><type>string</type><parameter>json</parameter></methodparam>
|
|
<methodparam choice="opt"><type class="union"><type>bool</type><type>null</type></type><parameter>associative</parameter><initializer>&null;</initializer></methodparam>
|
|
<methodparam choice="opt"><type>int</type><parameter>depth</parameter><initializer>512</initializer></methodparam>
|
|
<methodparam choice="opt"><type>int</type><parameter>flags</parameter><initializer>0</initializer></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
Konwertuje łańcuch znaków zakodowany jako JSON na wartość PHP.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>json</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Dekodowany <type>string</type> <parameter>json</parameter>.
|
|
</para>
|
|
<para>
|
|
Ta funkcja działa tylko z ciągami znaków w kodowaniu UTF-8.
|
|
</para>
|
|
&json.implementation.superset;
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>associative</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Jeśli ustawione na &true;, obiekty JSON będą zwrócone jako <link linkend="language.types.array">tablice</link>
|
|
asocjacyjne, jeśli ustawione na &false;, obiekty będą zwrócone jako <link linkend="language.types.object">obiekty</link>>.
|
|
Jeśli ustawiono na &null;, to obiekty będą zwrócone jako tablice asocjacyjne lub
|
|
obiekty, w zależności czy w parametrze <parameter>flags</parameter>
|
|
ustawiono <constant>JSON_OBJECT_AS_ARRAY</constant>.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>depth</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Maksymalna głębokość zagnieżdżenia dekodowanej struktury.
|
|
Wartość musi być większa niż <literal>0</literal>,
|
|
i mniejsza bądź równa <literal>2147483647</literal>.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>flags</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Maska bitowa z
|
|
<constant>JSON_BIGINT_AS_STRING</constant>,
|
|
<constant>JSON_INVALID_UTF8_IGNORE</constant>,
|
|
<constant>JSON_INVALID_UTF8_SUBSTITUTE</constant>,
|
|
<constant>JSON_OBJECT_AS_ARRAY</constant>,
|
|
<constant>JSON_THROW_ON_ERROR</constant>.
|
|
Zachowanie tych stałych opisano na
|
|
stronie <link linkend="json.constants">stałe JSON</link>.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
Zwraca wartość zakodowaną w <parameter>json</parameter> jako odpowiedni
|
|
typ PHP. Wartości <literal>true</literal>, <literal>false</literal>
|
|
i <literal>null</literal> bez cudzysłowów są zwracane odpowiednio jako &true;,
|
|
&false; i &null;. &null; jest zwracany, jeśli parametr
|
|
<parameter>json</parameter> nie może zostać odkodowany lub jeżeli zakodowane dane znajdują się
|
|
głębiej niż ustalony limit zagnieżdżania.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="errors">
|
|
&reftitle.errors;
|
|
<para>
|
|
Jeżeli parametr <parameter>depth</parameter> jest poza dozwolonym zakresem,
|
|
to od PHP 8.0.0 rzucany jest <classname>ValueError</classname>,
|
|
a wcześniej zgłaszany był błąd <constant>E_WARNING</constant>.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="changelog">
|
|
&reftitle.changelog;
|
|
<para>
|
|
<informaltable>
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>&Version;</entry>
|
|
<entry>&Description;</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>7.3.0</entry>
|
|
<entry>
|
|
Dodano flagę
|
|
<constant>JSON_THROW_ON_ERROR</constant>.
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>7.2.0</entry>
|
|
<entry>
|
|
Parametr <parameter>associative</parameter> dopuszcza teraz &null;.
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>7.2.0</entry>
|
|
<entry>
|
|
Dodano flagi
|
|
<constant>JSON_INVALID_UTF8_IGNORE</constant>, i
|
|
<constant>JSON_INVALID_UTF8_SUBSTITUTE</constant>.
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>7.1.0</entry>
|
|
<entry>
|
|
Pusty klucz JSON ("") może być prawidłowo zdekodowany do pustego
|
|
obiektu, zamiast używania klucza z wartością <literal>_empty_</literal>.
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title>Przykład użycia <function>json_decode</function></title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
|
|
|
|
var_dump(json_decode($json));
|
|
var_dump(json_decode($json, true));
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
object(stdClass)#1 (5) {
|
|
["a"] => int(1)
|
|
["b"] => int(2)
|
|
["c"] => int(3)
|
|
["d"] => int(4)
|
|
["e"] => int(5)
|
|
}
|
|
|
|
array(5) {
|
|
["a"] => int(1)
|
|
["b"] => int(2)
|
|
["c"] => int(3)
|
|
["d"] => int(4)
|
|
["e"] => int(5)
|
|
}
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
<example>
|
|
<title>Uzyskiwanie dostępu do właściwości nieprawidłowego obiektu</title>
|
|
<simpara>
|
|
Uzyskiwanie dostępu do elementów w obiekcie, który zawiera znaki
|
|
niedozwolone w konwencji nazewnictwa PHP (np. myślniki) może być
|
|
osiągnięte przez umieszczenie nazwy elementu wewnątrz nawiasów klamrowych lub apostrofów.
|
|
</simpara>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
$json = '{"foo-bar": 12345}';
|
|
|
|
$obj = json_decode($json);
|
|
print $obj->{'foo-bar'}; // 12345
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
<example>
|
|
<title>Różne przykłady błędów <function>json_decode</function></title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
// poniższe łańcucy są prawidłowe w JavaScript, ale nieprawidłowe w JSON
|
|
|
|
// nazwa i wartość musi być zawarta w podwójny cudzysłów
|
|
// pojedyńcze cudzysłowy nie są prawidłowe
|
|
$bad_json = "{ 'bar': 'baz' }";
|
|
json_decode($bad_json); // null
|
|
|
|
// nazwa musi być zawarta w podwójny cudzysłów
|
|
$bad_json = '{ bar: "baz" }';
|
|
json_decode($bad_json); // null
|
|
|
|
// przecinki na końcu nie są dozwolone
|
|
$bad_json = '{ bar: "baz", }';
|
|
json_decode($bad_json); // null
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
<example>
|
|
<title>Błędy - ustawianie głębokości zagnieżdżenia w <parameter>depth</parameter></title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
// Kodowanie danych z maksymalną głębokością 4 (array -> array -> array -> string)
|
|
$json = json_encode(
|
|
array(
|
|
1 => array(
|
|
'English' => array(
|
|
'One',
|
|
'January'
|
|
),
|
|
'Polish' => array(
|
|
'Jeden',
|
|
'Styczeń'
|
|
)
|
|
)
|
|
)
|
|
);
|
|
|
|
var_dump(json_decode($json, true, 4));
|
|
echo 'Ostatni <błąd></błąd>: ', json_last_error_msg(), PHP_EOL, PHP_EOL;
|
|
|
|
var_dump(json_decode($json, true, 3));
|
|
echo 'Ostatni błąd: ', json_last_error_msg(), PHP_EOL, PHP_EOL;
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
array(1) {
|
|
[1]=>
|
|
array(2) {
|
|
["English"]=>
|
|
array(2) {
|
|
[0]=>
|
|
string(3) "One"
|
|
[1]=>
|
|
string(7) "January"
|
|
}
|
|
["Polish"]=>
|
|
array(2) {
|
|
[0]=>
|
|
string(3) "Jeden"
|
|
[1]=>
|
|
string(7) "Styczeń"
|
|
}
|
|
}
|
|
}
|
|
Ostatni błąd: No error
|
|
|
|
NULL
|
|
Ostatni błąd: Maximum stack depth exceeded
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
<example>
|
|
<title>Dekodowanie <function>json_decode</function> - duże liczby</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$json = '{"number": 12345678901234567890}';
|
|
|
|
var_dump(json_decode($json));
|
|
var_dump(json_decode($json, false, 512, JSON_BIGINT_AS_STRING));
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
object(stdClass)#1 (1) {
|
|
["number"]=>
|
|
float(1.2345678901235E+19)
|
|
}
|
|
object(stdClass)#1 (1) {
|
|
["number"]=>
|
|
string(20) "12345678901234567890"
|
|
}
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="notes">
|
|
&reftitle.notes;
|
|
<note>
|
|
<para>
|
|
Specyfikacja JSON nie jest taka sama jak w JavaScript, tylko jest podzbiorem JavaScript.
|
|
</para>
|
|
</note>
|
|
<note>
|
|
<para>
|
|
W przypadku niepowodzenia dekodowania danych funkcja <function>json_last_error</function>
|
|
może zostać użyta do ścisłego ustalenia rodzaju błędu.
|
|
</para>
|
|
</note>
|
|
</refsect1>
|
|
|
|
<refsect1 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><function>json_encode</function></member>
|
|
<member><function>json_last_error</function></member>
|
|
<member><function>json_last_error_msg</function></member>
|
|
</simplelist>
|
|
</para>
|
|
</refsect1>
|
|
</refentry>
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|