0

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?

2
  • 1
    try jsonObject.results.length Commented Jan 22, 2017 at 5:29
  • 1
    Is your problem resolved? Commented Jan 22, 2017 at 5:57

3 Answers 3

2

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);

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.