Best way to parse JSON using JavaScript

Possible duplicate: Length of JavaScript object (i.e. 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)one that didn't work (just to find out how long the object is), and I also tried to (data.user[i])no avail.

I am basically very confused about how I can get the length of one of the objects in JSON and how I can render it efficiently. How can i do this?

+5
source share
5 answers

JSON, ( "age": 11 age: 11). / ( "firstName": "GGG", "age": 11 "firstName": "GGG" "age": 11. .

JSON, JSON.parse(data) , IE7, . :

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

IE7 , Douglas Crockford JSON polyfill: https://github.com/douglascrockford/JSON-js

+2

, json - http://jsonformatter.curiousconcept.com/

javascript script, script , . javascript

IE -

Firefox - firebug

+1

, , , ( ..), , . :

.

[]

data[0]. - .

[
    {}
]

, .

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

student data[0].student. .

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

:

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

. JavaScript

+1

-, json - , commsa , .. :

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

json , . :

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

, , .

0

, ,

data[i].student.name data[i].user.name

, ,

!!

0

All Articles