Lucas
Lucas

Reputation: 1279

How to access the data of this json response

Given this json response:

[
    {
        "diccioDatosForm": {
            "errorMessage": "Verifique los datos invalidos ingresados...",
            "encargadoLocalidad": "Ingrese un valor",
            "responseStatus": "ERR",
            "segundoNombre": "OK",
            "encargadoProvincia": "Ingrese un valor"
        }
    },
    {
        "listaEncargados": []
    }
]

I need to access the elements of the key 'diccioDatosForm'. When I print the json with an alert (alert(responseData)), I get:

[object Object],[object Object]

and I don't get anything when trying to do this:

alert(responseData.diccioDatosForm.errorMessage)

Upvotes: 1

Views: 108

Answers (4)

davidbuzatto
davidbuzatto

Reputation: 9444

It is because you are alerting the array that is being returned. To access the field you want, you should do:

responseData[0].diccioDatosForm.diccioDatosForm

I know that what I will say is not part of your question, but I suggest that you review your JSON structure, because its strange to have an array of two different things.

I would use something like this:

{
    "configs": {
        "segundoNombre": "OK",
        "encargadoProvincia": "Ingrese un valor",
        "encargadoLocalidad": "Ingrese un valor"
    },
    "error": {
        "message": "Verifique los datos invalidos ingresados..."
    },
    "itens": []   // "encargados" list here
}

Doing this you will have standard to use through yout application. To acess the message error you could do:

responseData.error.message

Upvotes: 1

Robert
Robert

Reputation: 8767

Your responseData object is an array with objects within. As a result you must use an index when referencing the internal objects.

responseData[0].diccioDatosForm.errorMessage

Upvotes: 2

rjz
rjz

Reputation: 16510

It looks like you're trying to find the value of a parameter that's in an object that's the first element of an array in your JSON. In plain Javascript, that means:

var data = [{"diccioDatosForm": {"errorMessage": /* ... */]

// grab diccioDatosForm from first array element:
var diccioDatosForm = data[0].diccioDatosForm;

Upvotes: 1

zerkms
zerkms

Reputation: 255115

Like this:

responseData[0].diccioDatosForm.errorMessage

responseData itself is an array contains of 2 elements

Upvotes: 5

Related Questions