Foros del Web » Programando para Internet » Jquery »

Problema al leer un JSON por Ajax

Estas en el tema de Problema al leer un JSON por Ajax en el foro de Jquery en Foros del Web. Hola, Al cargar por Ajax los datos de un json me pinta en pantalla el nombre, pero quiero que aparezca también el texto name. Es ...
  #1 (permalink)  
Antiguo 20/11/2013, 09:51
 
Fecha de Ingreso: marzo-2008
Mensajes: 186
Antigüedad: 16 años, 1 mes
Puntos: 3
Pregunta Problema al leer un JSON por Ajax

Hola,

Al cargar por Ajax los datos de un json me pinta en pantalla el nombre, pero quiero que aparezca también el texto name. Es decir en lugar de "Pepito" me salga "Name : Pepito" por ej.

Dejo el código del json y del script:

Código Javascript:
Ver original
  1. { "people" : [
  2.   {
  3.     "name" : "Ben",
  4.     "url" : "http://benalman.com/",
  5.     "bio" : "I create groovy websites, useful jQuery plugins, and play a mean funk bass. I'm also Director of Pluginization at @bocoup."
  6.   },
  7.   {
  8.     "name" : "Rebecca",
  9.     "url" : "http://rmurphey.com",
  10.     "bio" : "Senior JS dev at Bocoup"
  11.   },
  12.   {
  13.     "name" : "Jory",
  14.     "url" : "http://joryburson.com",
  15.     "bio" : "super-enthusiastic about open web education @bocoup. lover of media, art, and fake mustaches."
  16.   }
  17. ] }

Script de Ajax:

Código Javascript:
Ver original
  1. $(document).ready(function(){
  2.     $('a').click(function(e){
  3.         $.ajax({
  4.             url     :   'people.json',
  5.             data     :  "nocache=" + Math.random(),
  6.             dataType :  'json',
  7.             timeout  :  6000,
  8.             success  :  function(data){
  9.                             //JSON.stringify(data);
  10.                             $.each(data.people, function(key, value){
  11.                                 $('<li>' + key + ' : ' + value.name + '</li>').appendTo('ul');
  12.                                 console.log(key + " : " + value);
  13.                             });
  14.             },
  15.             error    :  function(data,status,error){
  16.                             console.log(status + error);
  17.             }
  18.         });
  19.         e.preventDefault();
  20.     });
  21. });

Soy iniciado en Ajax y no doy con la solución, aunque sé que tengo que modificar el key que muestro con el elemento <li>, espero vuestra ayuda.

Gracias.
  #2 (permalink)  
Antiguo 20/11/2013, 11:18
Avatar de Dalam  
Fecha de Ingreso: septiembre-2010
Mensajes: 409
Antigüedad: 13 años, 7 meses
Puntos: 56
Respuesta: Problema al leer un JSON por Ajax

Código Javascript:
Ver original
  1. $(document).ready(function(){
  2.     $('a').click(function(e){
  3.         $.ajax({
  4.             url     :   'people.json',
  5.             data     :  "nocache=" + Math.random(),
  6.             dataType :  'json',
  7.             timeout  :  6000,
  8.             success  :  function(data){
  9.                             //JSON.stringify(data);
  10.                             $.each(data.people, function(key, value){
  11.                                 $('<li>' + data.people[key] + ' : ' + value.name + '</li>').appendTo('ul');
  12.                                 console.log(key + " : " + value);
  13.                             });
  14.             },
  15.             error    :  function(data,status,error){
  16.                             console.log(status + error);
  17.             }
  18.         });
  19.         e.preventDefault();
  20.     });
  21. });
  #3 (permalink)  
Antiguo 21/11/2013, 09:01
 
Fecha de Ingreso: marzo-2008
Mensajes: 186
Antigüedad: 16 años, 1 mes
Puntos: 3
Respuesta: Problema al leer un JSON por Ajax

Gracias Dalam.

No me debí explicar bien, con el código que añades sale en pantalla esto:

Código HTML:
Ver original
  1. 0 : Ben
  2.     1 : Rebecca
  3.     2 : Jory

Y la idea es que salga así:

Código HTML:
Ver original
  1. Name : Ben
  2.     Name : Rebecca
  3.     Name : Jory

Name es la clave del JSON ¿se puede hacer de ese modo?

Nuevamente Dalam gracias por tu pronta respuesta.
  #4 (permalink)  
Antiguo 26/11/2013, 10:33
Avatar de Dalam  
Fecha de Ingreso: septiembre-2010
Mensajes: 409
Antigüedad: 13 años, 7 meses
Puntos: 56
Respuesta: Problema al leer un JSON por Ajax

Código Javascript:
Ver original
  1. $(document).ready(function(){
  2.     $('a').click(function(e){
  3.         $.ajax({
  4.             url     :   'people.json',
  5.             data     :  "nocache=" + Math.random(),
  6.             dataType :  'json',
  7.             timeout  :  6000,
  8.             success  :  function(data){
  9.                             //JSON.stringify(data);
  10.                             $.each(data.people, function(key, value){
  11.                                 $('<li>Name : ' + value.name + '</li>').appendTo('ul');
  12.                                 console.log(key + " : " + value);
  13.                             });
  14.             },
  15.             error    :  function(data,status,error){
  16.                             console.log(status + error);
  17.             }
  18.         });
  19.         e.preventDefault();
  20.     });
  21. });

Etiquetas: ajax, json
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 17:17.