Skip to main content

I am relatively new to javascript and very new to JSON and got the job of changing an internal website so it meets our new company needs. I am trying to access a JSON object with an array in it. This actually works pretty for most of the objects, except the array. Everytime I try to access the elements in the array I get undefined or NaN.

When I do

alert(JSON.stringify(object.data.average[0]));
I get the following output

[{"test_count2":"360","succeeded":"185"}]

so I know the objects I am trying to access are there, but when I do

alert(JSON.stringify(object.data.average[0].succeeded))
or

alert(JSON.stringify(object.data.average[0]['succeeded']))

I always get just

undefined

Am I doing something entirely wrong here? As I sad, I am a newby, so please be patient with me ;)

thanks in advance!

best regards!

ulchSD

I am relatively new to javascript and very new to JSON and got the job of changing an internal website so it meets our new company needs. I am trying to access a JSON object with an array in it. This actually works pretty for most of the objects, except the array. Everytime I try to access the elements in the array I get undefined or NaN.

When I do

alert(JSON.stringify(object.data.average[0]));
I get the following output

[{"test_count2":"360","succeeded":"185"}]

so I know the objects I am trying to access are there, but when I do

alert(JSON.stringify(object.data.average[0].succeeded))
or

alert(JSON.stringify(object.data.average[0]['succeeded']))

I always get just

undefined

Am I doing something entirely wrong here? As I sad, I am a newby, so please be patient with me ;)

thanks in advance!

best regards!

ulchSD

I am relatively new to javascript and very new to JSON and got the job of changing an internal website so it meets our new company needs. I am trying to access a JSON object with an array in it. This actually works pretty for most of the objects, except the array. Everytime I try to access the elements in the array I get undefined or NaN.

When I do

alert(JSON.stringify(object.data.average[0]));
I get the following output

[{"test_count2":"360","succeeded":"185"}]

so I know the objects I am trying to access are there, but when I do

alert(JSON.stringify(object.data.average[0].succeeded))
or

alert(JSON.stringify(object.data.average[0]['succeeded']))

I always get just

undefined

Am I doing something entirely wrong here? As I sad, I am a newby, so please be patient with me ;)

thanks in advance!

Source Link
ulchSD5922
  • 113
  • 1
  • 6

Accessing array elment in JSON object is undefined

I am relatively new to javascript and very new to JSON and got the job of changing an internal website so it meets our new company needs. I am trying to access a JSON object with an array in it. This actually works pretty for most of the objects, except the array. Everytime I try to access the elements in the array I get undefined or NaN.

When I do

alert(JSON.stringify(object.data.average[0]));
I get the following output

[{"test_count2":"360","succeeded":"185"}]

so I know the objects I am trying to access are there, but when I do

alert(JSON.stringify(object.data.average[0].succeeded))
or

alert(JSON.stringify(object.data.average[0]['succeeded']))

I always get just

undefined

Am I doing something entirely wrong here? As I sad, I am a newby, so please be patient with me ;)

thanks in advance!

best regards!

ulchSD