JavaScript json loop item in array

时间:2023-02-06 14:57:28

Iterating through/Parsing JSON Object via JavaScript

解答1

Your JSON object is incorrect because it has multiple properties with the same name. You should be returning an array of "student" objects.

[
{
"id": 456,
"full_name": "GOOBER ANGELA",
"user_id": "2733245678",
"stin": "2733212346"
},
{
"id": 123,
"full_name": "BOB, STEVE",
"user_id": "abc213",
"stin": "9040923411"
}
]

Then you can iterate over it as so:

 for (var i = 0, len = objJSON.length; i < len; ++i) {
var student = objJSON[i];
$("<div id=\"" + student.id + "\">" + student.full_name + " (" + student.user_id + " - " + student.stin + ")</div>")...
}

解答2

var data = '[
{
"id": 456,
"full_name": "GOOBER, ANGELA",
"user_id": "2733245678",
"stin": "2733212346"
},
{
"id": 123,
"full_name": "BOB, STEVE",
"user_id": "abc213",
"stin": "9040923411"
}
]'; $.each(data, function(index, val) {
alert(val.id);
alert(val.full_name);
alert(val.user_id);
alert(val.stin);
})