讀取嵌套 json 顯示每個鍵值 [來自 mySQL]
我正在嘗試讀取嵌套的 JSON 數(shù)據(jù),其中鍵值是子嵌套的鍵和值,但它似乎不起作用。數(shù)據(jù)庫表中的第一個 json 數(shù)據(jù) [列:角色]$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 jsonconsole.log(json.role.length) // undefined// for loop not worksfor(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){ //...... } }}
查看完整描述