我正在尝试读取嵌套的 JSON 数据,其中键值是子嵌套的键和值,但它似乎不起作用。数据库表中的第一个 json 数据 [列:角色]
$json = array();
$sql = $db->query("SELECT * FROM u_info WHERE bid=".$branch);
while ($rs = $sql->fetch_assoc()) {
$rs['img'] = getImg($rs['img']);
$query = $db->query('SELECT roles FROM wp_roles WHERE userid='.$rs['id'])->fetch_assoc();
$rs['role'] = json_decode($query['roles']); // already a json format
$json[] = $rs;
}
exit(json_encode($json));// convert to json AJAX response works
然后json结果如下
{
"academics":{
"class":"true",
"employee":"false",
"students":"true",
"subject":"false",
"all":"true"
},
"exam":{
"exams":"false",
"schedule":"false",
"result":"false",
"marksheet":"false",
"all":"false"
},
"timetable":{
"class":"false",
"teacher":"false",
"all":"false"
},
"attendance":{
"students":"true",
"teacher":"true",
"all":"true"
}
}
未定义的 json 长度 [JAVASCRIPT]
// parse nested json
var json = JSON.parse(data)
console.log(json.role) // works and print above json
console.log(json.role.length) // undefined
// for loop not works
for(i = 0; i < json.role.length; i++){
for(y = 0; y < json.role[i].length; y++){
// json.role.academics.class === true [if condition]
if(json.role[i][y] === true){
//......
}
}
}
大话西游666
相关分类