Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

79
Vistas
How to parse Json object in Jquery

I have a little experience in JSON I did it on my Android application now using JSON again in my webpage as AJAX reponse, I did research about ajax and found a tutorial to get my data in database using JSON so I tried but I didn't know how to parse the object.

My Jquery Code.

$.ajax({
    type: 'GET',
    dataType: 'json',
    url: 'functions/json.php',
    success: function(response){
   var json = $.parseJSON(response);
   alert(json.firstname) //where my response is $response['firstname']
},
error: function(data){
   var json = $.parseJSON(data);
   alert(json.error);
}
});

Using the php I echo the jsonArray as json_encode and heres the json output

{"id":"2","firstname":"john","lastname":"Doe"}

using google chrome console i got this error

Uncaught SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)

when the function response output as alert(reponse) output was

[object Object]
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Don't parse it. You've told jQuery to:

dataType: "json"

So response is the parsed object, not JSON. Just use it directly:

$.ajax({
    type: 'GET',
    dataType: 'json',
    url: 'functions/json.php',
    success: function(response){
        alert(response.firstname);
    },
    error: function(data) {
        // `data` will not be JSON
    }
});

Also note that the first parameter to the error callback will not be JSON or the result of parsing JSON in the error callback.

See the documentation for details.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda