mirror of
https://github.com/php/doc-es.git
synced 2026-03-26 00:12:06 +01:00
git-svn-id: https://svn.php.net/repository/phpdoc/es/trunk@334304 c90b9560-bf6c-de11-be94-00142212c4b1
354 lines
8.8 KiB
XML
354 lines
8.8 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: 6353c52d0ee11ceb942e857b001e95d85500d690 Maintainer: seros Status: ready -->
|
|
<!-- Reviewed: yes Maintainer: seros -->
|
|
<refentry xml:id="function.json-decode" xmlns="http://docbook.org/ns/docbook">
|
|
<refnamediv>
|
|
<refname>json_decode</refname>
|
|
<refpurpose>Decodifica un string de 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>bool</type><parameter>assoc</parameter><initializer>false</initializer></methodparam>
|
|
<methodparam choice="opt"><type>int</type><parameter>depth</parameter><initializer>512</initializer></methodparam>
|
|
<methodparam choice="opt"><type>int</type><parameter>options</parameter><initializer>0</initializer></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
Convierte un string codificado en JSON a una variable de PHP.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>json</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
El <type>string</type> de <parameter>json</parameter> a decodificar.
|
|
</para>
|
|
<para>
|
|
Esta función sólo trabaja con string codificados en UTF-8.
|
|
</para>
|
|
&json.implementation.superset;
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>assoc</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Cuando es &true;, los <type>object</type> devueltos serán convertidos a
|
|
<type>array</type> asociativos.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>depth</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Profundidad de recursividad especificada por el usuario.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>options</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Máscara de bit de opciones de decodificación de JSON. Actualmente solamente
|
|
está soportada <constant>JSON_BIGINT_AS_STRING</constant>
|
|
(por defecto, convierte enteros grandes en valores de tipo float)
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
Devuelve el valor codificado en <parameter>json</parameter> en un tipo de PHP
|
|
apropiado. Los valores <literal>true</literal>, <literal>false</literal> y
|
|
<literal>null</literal> son devueltos como &true;, &false; y &null;
|
|
respectivamente. &null; es devuelto si el parámetro <parameter>json</parameter> no se puede
|
|
decodificar o si los datos codificados son más profundos que el límite de recursividad.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title>Ejemplos de <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>Acceder a propiedades de objeto no válidas</title>
|
|
<simpara>
|
|
Se puede acceder a elementos de un objeto que contienen caracteres no
|
|
permitidos por la convención de nombres de PHP (p.ej., el guión)
|
|
encapsulando el nombre del elemento entre llaves y apóstrofe.
|
|
</simpara>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
$json = '{"foo-bar": 12345}';
|
|
|
|
$obj = json_decode($json);
|
|
print $obj->{'foo-bar'}; // 12345
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
<example>
|
|
<title>Errores comunes al usar <function>json_decode</function></title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
// los siguientes string son válidos en JavaScript pero no en JSON
|
|
|
|
// el nombre y el valor deben estar entre comillas dobles
|
|
// las comillas simples no son válidas
|
|
$json_erróneo = "{ 'bar': 'baz' }";
|
|
json_decode($json_erróneo); // null
|
|
|
|
// el nombre debe estar entre comillas dobles
|
|
$json_erróneo = '{ bar: "baz" }';
|
|
json_decode($json_erróneo); // null
|
|
|
|
// las comas finales no están permitidas
|
|
$json_erróneo = '{ bar: "baz", }';
|
|
json_decode($json_erróneo); // null
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
<example>
|
|
<title>Errores con <parameter>depth</parameter></title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
// Codificar los datos.
|
|
$json = json_encode(
|
|
array(
|
|
1 => array(
|
|
'Inglés' => array(
|
|
'One',
|
|
'January'
|
|
),
|
|
'Francés' => array(
|
|
'Une',
|
|
'Janvier'
|
|
)
|
|
)
|
|
)
|
|
);
|
|
|
|
// Definir los errores.
|
|
$constantes = get_defined_constants(true);
|
|
$errores_json = array();
|
|
foreach ($constantes["json"] as $nombre => $valor) {
|
|
if (!strncmp($nombre, "JSON_ERROR_", 11)) {
|
|
$errores_json[$valor] = $nombre;
|
|
}
|
|
}
|
|
|
|
// Mostrar los errores para diferentes profundidades.
|
|
foreach (range(4, 3, -1) as $profundidad) {
|
|
var_dump(json_decode($json, true, $profundidad));
|
|
echo 'Último error: ', $errores_json[json_last_error()], PHP_EOL, PHP_EOL;
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
array(1) {
|
|
[1]=>
|
|
array(2) {
|
|
["Inglés"]=>
|
|
array(2) {
|
|
[0]=>
|
|
string(3) "One"
|
|
[1]=>
|
|
string(7) "January"
|
|
}
|
|
["Francés"]=>
|
|
array(2) {
|
|
[0]=>
|
|
string(3) "Une"
|
|
[1]=>
|
|
string(7) "Janvier"
|
|
}
|
|
}
|
|
}
|
|
Último error: JSON_ERROR_NONE
|
|
|
|
NULL
|
|
Último error: JSON_ERROR_DEPTH
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
<example>
|
|
<title><function>json_decode</function> de enteros grandes</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>
|
|
La especificación de JSON no es JavaScript, pero sí un subconjunto de JavaScript.
|
|
</para>
|
|
</note>
|
|
<note>
|
|
<para>
|
|
En el caso de ocurrir un error durante la decodificación, se puede usar
|
|
<function>json_last_error</function> para determinar la naturaleza exacta del mismo.
|
|
</para>
|
|
</note>
|
|
</refsect1>
|
|
|
|
<refsect1 role="changelog">
|
|
&reftitle.changelog;
|
|
<para>
|
|
<informaltable>
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>&Version;</entry>
|
|
<entry>&Description;</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>5.6.0</entry>
|
|
<entry>
|
|
Ya no se aceptan variantes que no estén en minúsculas de los literales
|
|
<literal>true</literal>, <literal>false</literal> y <literal>null</literal>
|
|
como entradas válidas, por lo que se generarán advertencias.
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>5.4.0</entry>
|
|
<entry>
|
|
Se añadió el parámetro <parameter>options</parameter>.
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>5.3.0</entry>
|
|
<entry>Se añadió el parámetro opcional <parameter>depth</parameter>. La profundidad de recursividad predeterminada se aumentó de 128 a 512</entry>
|
|
</row>
|
|
<row>
|
|
<entry>5.2.3</entry>
|
|
<entry>La profundidad de recursividad predeterminada se aumentó de 20 a 128</entry>
|
|
</row>
|
|
<row>
|
|
<entry>5.2.1</entry>
|
|
<entry>
|
|
Se añadió soporte para la decodificación de JSON de tipos básicos.
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><function>json_encode</function></member>
|
|
<member><function>json_last_error</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
|
|
-->
|