-1

I am trying to access json array but still failed. I have following array

{
    "response": {
        "error": [
            "name is already exists",
            "email@gmail.com is already exists"
        ]
    }
}

I know this is very common question and already there are hundreds of answers and tutorials but i am still failed. So can someone kindly guide me. I would like to appreciate.

I have tried following script but it showing undefined

response.error
response.error[0]
response['error'][0]
Muiz Ahmed
  • 61
  • 6

2 Answers2

2

Try code the example below.

var json = {
    "response": {
        "error": [
            "name is already exists",
            "email@gmail.com is already exists"
        ]
    }
};

alert(json.response.error[0]);
alert(json.response.error[1]);
Hung Quach
  • 2,079
  • 2
  • 17
  • 28
0

You are not pointing to JavaScript the object. You need to create a variable, and associate to your object.

Example: variableName.response.error

For more info: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON