Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have the following JSON information. It is an array nested inside an object. I am trying to access components of the object, however I cant seem to figure out how to work with the object.

For instance, when I try to get the number of events in the array (there should be 2), jsonObject.length returns the character count. where as jsonObject[0].length returns one, which in my opinion should be 2 (2 events).

{"results":[{"eventName":"Rey","name":"Tar","dateOfShow":"2017-01-27T22:00:00","userGroupName":"Bit","eventHallName":"Grnn","imageSource":"test2.jpg"},{"eventName":"Gor","name":"Skum","dateOfShow":"2017-01-30T20:00:00","userGroupName":"Gaf","eventHallName":"Gai","imageSource":"test1.jpg"}]}

I have worked with JSON arrays before by parsing them, however when I parse this, it returns undefined for any value.

How would I get the number of events displayed in this JSON object using javascript?

share
1  
try jsonObject.results.length – Jyothi Babu Araja 1 hour ago
    
Is your problem resolved? – Zeeshan Bilal 1 hour ago

Use JSON.parse(jsonObject); and then try to access. json.results it will give you the array of length two.

var jsonString = '{"results":[{"eventName":"Rey","name":"Tar","dateOfShow":"2017-01-27T22:00:00","userGroupName":"Bit","eventHallName":"Grnn","imageSource":"test2.jpg"},{"eventName":"Gor","name":"Skum","dateOfShow":"2017-01-30T20:00:00","userGroupName":"Gaf","eventHallName":"Gai","imageSource":"test1.jpg"}]}';
var jsonObject = JSON.parse (jsonString);
jsonObject.results.length; // length of results array
jsonObject.results[0]; //first object of array
share|improve this answer

Try like this.

JSON is string.Directly javascript's length property will not works.You have convert it to object first using JSON.parse().

json ='{"results":[{"eventName":"Rey","name":"Tar","dateOfShow":"2017-01-27T22:00:00","userGroupName":"Bit","eventHallName":"Grnn","imageSource":"test2.jpg"},{"eventName":"Gor","name":"Skum","dateOfShow":"2017-01-30T20:00:00","userGroupName":"Gaf","eventHallName":"Gai","imageSource":"test1.jpg"}]}';
res = JSON.parse(json)
alert(res.results.length);

share|improve this answer

I think,this will helps you

  json ='{"results":[{"eventName":"Rey","name":"Tar","dateOfShow":"2017-01-27T22:00:00","userGroupName":"Bit","eventHallName":"Grnn","imageSource":"test2.jpg"},{"eventName":"Gor","name":"Skum","dateOfShow":"2017-01-30T20:00:00","userGroupName":"Gaf","eventHallName":"Gai","imageSource":"test1.jpg"}]}';
res = JSON.parse(json)
alert(res.results.length);
var i = 0;
while(i<res.results.length)
{
    alert(res.results[i].eventName)
    i++;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.