0

I want to parse following json in php and get all the values

{"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"
}
4
  • what have you tried so far. btw you might try looking at json_encode / json_decode functions Commented Mar 9, 2014 at 6:08
  • Can you more clear on what you want to do? You could use json_decode() function and then access the values Commented Mar 9, 2014 at 6:12
  • my question is after json_decode() how to parse the values of student, class and user name Commented Mar 9, 2014 at 6:20
  • 1
    After json_decode you don't need to "parse" anything, it's just PHP arrays and objects that you access normally. Commented Mar 9, 2014 at 6:30

2 Answers 2

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/>";
Sign up to request clarification or add additional context in comments.

Comments

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>";
}

2 Comments

its working and i get the values of adm_no but how to get the values class and username
echo $array->class; echo $array->username;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.