Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Acceso a Datos Fichero JSON (Sencillo)

Tiempo de lectura: 2 minutos

Buenas tardes a tod@s os voy a dejar un tutorial de como acceder a los datos:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<body>
<h2>Primer ejemplo Acceso a datos formato JSON</h2>
<p id="miDiv"></p>
<script>
var texto = '[{"nombre":"Laura Gonzalez", "calle":"Su casa en la montania portal 6", "telefono":877436700, "fecha":"1998-07-25"},{"nombre":"Pablo Perez", "calle":"Su casa en la playa bajo D", "telefono":637456321, "fecha":"1998-07-26"}]';
var obj = JSON.parse(texto);
var respuesta = obj[0]["nombre"] + "<br>" + obj[0].calle + "<br>" + obj[0].telefono + "<br>" + new Date(obj[0].fecha);
respuesta += "<hr>";
respuesta += obj[1]["nombre"] + "<br>" + obj[1].calle + "<br>" + obj[1].telefono + "<br>" + new Date(obj[1].fecha);
document.getElementById("miDiv").innerHTML = respuesta;
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <h2>Primer ejemplo Acceso a datos formato JSON</h2> <p id="miDiv"></p> <script> var texto = '[{"nombre":"Laura Gonzalez", "calle":"Su casa en la montania portal 6", "telefono":877436700, "fecha":"1998-07-25"},{"nombre":"Pablo Perez", "calle":"Su casa en la playa bajo D", "telefono":637456321, "fecha":"1998-07-26"}]'; var obj = JSON.parse(texto); var respuesta = obj[0]["nombre"] + "<br>" + obj[0].calle + "<br>" + obj[0].telefono + "<br>" + new Date(obj[0].fecha); respuesta += "<hr>"; respuesta += obj[1]["nombre"] + "<br>" + obj[1].calle + "<br>" + obj[1].telefono + "<br>" + new Date(obj[1].fecha); document.getElementById("miDiv").innerHTML = respuesta; </script> </body> </html>
<!DOCTYPE html>
<html>

<body>

    <h2>Primer ejemplo Acceso a datos formato JSON</h2>

    <p id="miDiv"></p>

    <script>
        var texto = '[{"nombre":"Laura Gonzalez", "calle":"Su casa en la montania portal 6", "telefono":877436700, "fecha":"1998-07-25"},{"nombre":"Pablo Perez", "calle":"Su casa en la playa bajo D", "telefono":637456321, "fecha":"1998-07-26"}]';
        var obj = JSON.parse(texto);
        var respuesta = obj[0]["nombre"] + "<br>" + obj[0].calle + "<br>" + obj[0].telefono + "<br>" + new Date(obj[0].fecha);
        respuesta += "<hr>";
        respuesta += obj[1]["nombre"] + "<br>" + obj[1].calle + "<br>" + obj[1].telefono + "<br>" + new Date(obj[1].fecha);
        document.getElementById("miDiv").innerHTML = respuesta;
    </script>

</body>

</html>

En primer lugar lo que hacemos es generar un JSON, en mi caso almacenado en la variable texto

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var texto = '[{"nombre":"Laura Gonzalez", "calle":"Su casa en la montania portal 6", "telefono":877436700, "fecha":"1998-07-25"},{"nombre":"Pablo Perez", "calle":"Su casa en la playa bajo D", "telefono":637456321, "fecha":"1998-07-26"}]';
var texto = '[{"nombre":"Laura Gonzalez", "calle":"Su casa en la montania portal 6", "telefono":877436700, "fecha":"1998-07-25"},{"nombre":"Pablo Perez", "calle":"Su casa en la playa bajo D", "telefono":637456321, "fecha":"1998-07-26"}]';
        var texto = '[{"nombre":"Laura Gonzalez", "calle":"Su casa en la montania portal 6", "telefono":877436700, "fecha":"1998-07-25"},{"nombre":"Pablo Perez", "calle":"Su casa en la playa bajo D", "telefono":637456321, "fecha":"1998-07-26"}]';

A continuación, pasamos la variable que contiene el JSON, para obtener una variable o un objeto

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var obj = JSON.parse(texto);
var obj = JSON.parse(texto);
        var obj = JSON.parse(texto);

Ahora creamos una variable respuesta, que es donde vamos a ir mostrando los datos que obtenemos de ese JSON /tenemos que mirarlo como si estuviésemos accediendo a los datos de un array

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var respuesta = obj[0]["nombre"] + "<br>" + obj[0].calle + "<br>" + obj[0].telefono + "<br>" + new Date(obj[0].fecha);
respuesta += "<hr>";
respuesta += obj[1]["nombre"] + "<br>" + obj[1].calle + "<br>" + obj[1].telefono + "<br>" + new Date(obj[1].fecha);
var respuesta = obj[0]["nombre"] + "<br>" + obj[0].calle + "<br>" + obj[0].telefono + "<br>" + new Date(obj[0].fecha); respuesta += "<hr>"; respuesta += obj[1]["nombre"] + "<br>" + obj[1].calle + "<br>" + obj[1].telefono + "<br>" + new Date(obj[1].fecha);
        var respuesta = obj[0]["nombre"] + "<br>" + obj[0].calle + "<br>" + obj[0].telefono + "<br>" + new Date(obj[0].fecha);
        respuesta += "<hr>";
        respuesta += obj[1]["nombre"] + "<br>" + obj[1].calle + "<br>" + obj[1].telefono + "<br>" + new Date(obj[1].fecha);

Y por último, con la propiedad .InnerHTML, mostramos la variable de respuesta que almacena el resultado de todo el procedimiento:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
document.getElementById("miDiv").innerHTML = respuesta;
document.getElementById("miDiv").innerHTML = respuesta;
        document.getElementById("miDiv").innerHTML = respuesta;

Y el resultado es el siguiente:

¡OJO! cuando en la variable de respuesta, o en cualquier otra variable, ponemos el símbolo += (respuesta+=) Lo que estamos haciendo, es concatenar todos los datos o String que añadamos en la misma variable, sin necesidad de tener varias variables.

2

Deja un comentario