5

Possible Duplicate: Length of a JavaScript object (that is, associative array)

I have JSON in the following format

[{"student":{"name" : "ABCD",
        "age":8,
        }
    "user":{ "firstName":"ABCD",
            "age": 9,
        }
        },
   {"student":{"name" : "XCYS",
        "age":10,
        }
    "user":{ "firstName":"GGG",
            "age": 11,
        }
},]

I tried using (data.student[i].length), which did not work (just to see what the length of the object is), and I also tried (data.user[i]) to no avail.

I am basically very confused on how I can get the length of one of the objects in the JSON and how I can effectively display it. How can I do this?

Community
  • 1
  • 1
alex
  • 235
  • 3
  • 11
  • http://stackoverflow.com/questions/5223/length-of-javascript-object-ie-associative-array – Dr. Dan Aug 06 '12 at 07:26
  • 2
    That's not valid JSON and not a valid array or object literal - you're missing some commas. Assuming you fix that, how you access it depends on whether it actually is JSON - a string that needs to be parsed with `JSON.parse(yourJSONhere)`, or if it's like `var data = [{"student":""}];`, an array literal, _not_ JSON. Given that you've got an array, you'd need something like `data[i].student`. – nnnnnn Aug 06 '12 at 07:32

5 Answers5

2

The content in your questions doesn't parse as valid JSON as all keys need to be wrapped in quotes (so you should have "age": 11 instead of age: 11). All key/value pairs in an object should be separated by a comma (so you should have "firstName": "GGG", "age": 11 instead of "firstName": "GGG" "age": 11. You also have a trailing comma in the outer array.

So long as you have valid JSON, you should be able to use JSON.parse(data) in all browsers newer than IE7 to convert the string into an actual object. Once parsed into an object, you can use:

var data = "your JSON string";
var object = JSON.parse(data);

console.log(object.length); // the number of objects in the array
console.log(object[0].student.name; // the name of the first student

If you are supporting browsers IE7 and older, check out Douglas Crockford's JSON polyfill: https://github.com/douglascrockford/JSON-js

steveukx
  • 4,370
  • 19
  • 27
  • 1
    "Can I Use" has a more complete list of browsers that support JSON: http://caniuse.com/#feat=json – steveukx Aug 06 '12 at 07:43
1

Please validate your json string in some websites http://jsonformatter.curiousconcept.com/

Since javascript is a script language, it will interpret the script and terminate the interpretation at the moment when it has some syntax errors. Try to use some javascript console in your browser

IE - Developer Tools

Firefox - firebug plugin

Community
  • 1
  • 1
sundar
  • 1,760
  • 12
  • 28
1

Let's assume that the actual object you have is valid (no missing commas etc), and you've parsed the string correctly, you're trying to access objects with array indexes and vice versa. Let's rebuild the object from ground up:

First, you have an array.

[]

You access the first element in the array using data[0]. The first element is an object.

[
    {}
]

The object has two keys, which both hold objects.

[
    {
        "student": {},
        "user": {}
    }
]

You access the student key with data[0].student. It contains and object with two keys.

[
    {
        "student": {
            "name": "ABCD",
            "age": 8
        },
        "user": {}
    }
]

You get the number of values in that object with:

Object.keys( data[0].student ).length;

For better browser support see Length of a JavaScript object

Community
  • 1
  • 1
JJJ
  • 32,902
  • 20
  • 89
  • 102
0

first, the json is not structured correctly - some element names are missing quotes, commsa in the wrong places, missing, etc. Compare yours to this:

 var json = [{"student":{"name" : "ABCD","age":8}, "user":{ "firstName":"ABCD", "age": 9}},
            {"student":{"name" : "XCYS","age":10}, "user":{ "firstName":"GGG", "age": 11}}
            ];

json is easier to proof read that way than spread out vertically. Then, with a simple loop:

    for(var ix in json)
{
        alert(json[ix].student.name +"="+json[ix].student.age);
}

hopefully you can see how to go from there.

Serexx
  • 1,232
  • 1
  • 15
  • 32
0

I figured out how to parse it , seems very simple

I used data[i].student.name and data[i].user.name

this seems to alert the correct value

Thank you all for the responses !!

alex
  • 235
  • 3
  • 11