我正在尝试解析以下内容:
[{"Student":{"student_id":"19","student_name":"Test"},"Teacher":{"teacher_id":"1"}}]
但我不确定获取值19,Test和1的语法,请帮助。
答案 0 :(得分:0)
尝试,
var test = '[{"Student":{"student_id":"19","student_name":"Test"},"Teacher":{"teacher_id":"1"}}]';
var obj = jQuery.parseJSON(test);
console.log(obj[0].Teacher.teacher_id)
答案 1 :(得分:0)
在PHP中:
$json = '[{"Student":{"student_id":"19","student_name":"Test"},"Teacher":{"teacher_id":"1"}}]
';
$data = json_decode($json);
// You can access values like this:
$student = $data[0]->Student;
$student_id = $student->student_id;
$student_name = $student->student_name;
$teacher_id = $data[0]->Teacher->teacher_id;
在Javascript中:
var json = '[{"Student":{"student_id":"19","student_name":"Test"},"Teacher":{"teacher_id":"1"}}]
';
var data;
if (window.JSON) {
data = JSON.parse(json);
} else {
data = eval(json);
}
// You can access values like this:
var student = data[0].Student;
var student_id = student.student_id;
var student_name = sudent.student_name;
var teatcher_id = data[0].Teacher.teacher_id;
答案 2 :(得分:0)
var persons = JSON.parse('[{"Student":{"student_id":"19","student_name":"Test"},"Teacher":{"teacher_id":"1"}}]');
var myStudentID = persons[0].Student.student_id;
var myStudentTest = persons[0].Student.Test;
var myTeacherID = persons[0].Teacher.teacher_id;
我觉得这是家庭作业