[{
"SchoolId":"015-08-0034-009-37",
"SubjectId":"08-0034-00613",
"Student":[
{"StudentId":"T-15981","StudentName":"John"},
{"StudentId":"T-15982","StudentName":"Paul"}
]
}]
我的php json_encode中有一个像这样的json格式。我正在获取这样的数据
for (var i = 0; i < data.length; i++) {
console.log(data.[i].SchoolId);
console.log(data.[i].SubjectId);
}
我希望获得
的价值 {"StudentId":"T-15981","StudentName":"John"},
{"StudentId":"T-15982","StudentName":"Paul"}
如何获得两个条目的价值?赞赏任何想法
UPDATE
success: function(data) {
for (var i = 0; i < data.student.length; i++) {
console.log(data.student[i].StudentId);
console.log(data.student[i].SchoolId);
}
},
这是我通过将数据类型从json更改为html而从ajax响应获得的print_r。这是network>XHR>Response
中的输出
Array
(
[0] => Array
(
[SchoolId] => 015-08-0034-009-37
[SubjectId] => 08-0034-00613
[Student] => Array
(
[0] => Array
(
[StudentId] => 015-08-0034-009-37
[firstname] => Chona
[lastname] => Sy
[middleinitial] => D
)
[1] => Array
(
[StudentId] => 015-08-0034-009-37
[firstname] => Alona
[lastname] => Sy
[middleinitial] => D
)
)
)
[1] => Array
(
[SchoolId] => 015-08-0034-009-38
[SubjectId] => 08-0034-00613
[SupersededProperty] => Array
(
[0] => Array
(
[StudentId] => 015-08-0034-009-36
[firstname] => Edith
[lastname] => Sy
[middleinitial] => D
)
)
)
)
答案 0 :(得分:1)
您可以在FIDDLE
中找到工作示例JS代码
var e = {
"Student":[
{"StudentId":"T-15981","StudentName":"John"},
{"StudentId":"T-15982","StudentName":"Paul"}
],
"SchoolId":"015-08-0034-009-37",
"SubjectId":"08-0034-00613",
};
for(var i=0; i<e.Student.length; i++){
alert('StudentId = ' + e.Student[i].StudentId + '; StudentName = ' + e.Student[i].StudentName);
}
答案 1 :(得分:0)
var data = [{
"SchoolId":"015-08-0034-009-37",
"SubjectId":"08-0034-00613",
"Student":[
{"StudentId":"T-15981","StudentName":"John"},
{"StudentId":"T-15982","StudentName":"Paul"}
]
}]
for(var i=0; i<data[0].Student.length; i++){
var StudentData = data[0].Student[i];
alert(StudentData.StudentId);
}
答案 2 :(得分:0)
问题是你在数组中有一个数组。这导致json周围的方括号[{...}]。
Array
(
[0] => Array
(
...
你不应该回应第一个阵列。
例如,现在看起来像是
echo json_encode(array($data));
而你应该
echo json_encode($data);
作为最后的resorst尝试回显$ data [0];
另外see this