如何在php中解析以下json字符串

时间:2014-03-09 06:06:25

标签: php json foreach

我想解析php中的json并获取所有值

{"Student":
 [
 {  "adm_no":"101/1", "teacher_id":"4", "attendance":0},
 {  "adm_no":"101/6", "teacher_id":"4", "attendance":0 },
 {  "adm_no":"12/12", "teacher_id":"4", "attendance":0},
 {  "adm_no":"14/12", "teacher_id":"4", "attendance":0},
 { "adm_no":"147/1", "teacher_id":"4", "attendance":0 },
 {  "adm_no":"45/12", "teacher_id":"4", "attendance":0}
  ],
 "class":"s3b",
"username":"4"
}

2 个答案:

答案 0 :(得分:1)

$json = '{"Student":
   [
   {  "adm_no":"101/1", "teacher_id":"4", "attendance":0},
   {  "adm_no":"101/6", "teacher_id":"4", "attendance":0 },
   {  "adm_no":"12/12", "teacher_id":"4", "attendance":0},
   {  "adm_no":"14/12", "teacher_id":"4", "attendance":0},
   { "adm_no":"147/1", "teacher_id":"4", "attendance":0 },
   {  "adm_no":"45/12", "teacher_id":"4", "attendance":0}
    ],
   "class":"s3b",
  "username":"4"
  }';
$out = json_decode($json);
foreach($out->Student as $adm){
echo "Adm_no: ".$adm->adm_no."<br/>";
}
echo "Class : ".$out->class."<br/>";
echo "Username : ".$out->username."<br/>";

答案 1 :(得分:0)

$json = '{"Student":
   [
   {  "adm_no":"101/1", "teacher_id":"4", "attendance":0},
   {  "adm_no":"101/6", "teacher_id":"4", "attendance":0 },
   {  "adm_no":"12/12", "teacher_id":"4", "attendance":0},
   {  "adm_no":"14/12", "teacher_id":"4", "attendance":0},
   { "adm_no":"147/1", "teacher_id":"4", "attendance":0 },
   {  "adm_no":"45/12", "teacher_id":"4", "attendance":0}
    ],
   "class":"s3b",
  "username":"4"
  }';
$array = json_decode($json);
foreach($array->Student as $std){
 echo "Adm_no: ".$std->adm_no."<br>";
}