Valid way to Generate Nested or Complex JSON data using PHP for Flutter

We used to build chrome extension and worked with JSON and Javascript, at that time We have certain ways to generate Nested JSON with PHP but, when We come to Flutter App Development, that causes an error while parsing. After that, We get another way; certainly, a valid way to generate the JSON. We have shown that method in the example below.

Database Table:

PHP Code:

<?php 
  $db = "test_db";
  $host = "localhost";
  $db_user = 'root';
  $db_password = 'root';
  //MySql server and database info

  $link = mysqli_connect($host, $db_user, $db_password, $db);
  //connecting to database

  $json["error"] = false; //boolean output
  $json["errmsg"] = "";
  $json["data"] = array();
  //json output array

  $sql = "SELECT * FROM student_list ORDER BY full_name ASC";
  $res = mysqli_query($link, $sql);
  $numrows = mysqli_num_rows($res);
  if($numrows > 0){
      
     //check if there is any data
      while($array = mysqli_fetch_assoc($res)){
           array_push($json["data"], $array);
      }

      /* Do not push data like below,
      it will cause error on flutter json prasing. 
      user array_push() function.  

      $datalist = array();
      $x = 1;
      while($array = mysqli_fetch_assoc($res)){
           $datalist[$x] = $array;
           $x++;
      }
      $json["data"] = $datalist;

      */
  }else{
      $json["error"] = true;
      $json["errmsg"] = "No any data to show.";
  }
  
  mysqli_close($link);

  header('Content-Type: application/json');
  // tell browser that its a json data
  echo json_encode($json);

?>

JSON Output:

{
   "error":false,
   "errmsg":"",
   "data":[
      {
         "student_id":"4",
         "full_name":"Anil Chaudhary",
         "address":"Kailali, Nepal",
         "class":"Nine",
         "roll_no":"9",
         "dob":"1997-02-28"
      },
      {
         "student_id":"5",
         "full_name":"Arjun Chaudhary",
         "address":"Chaumala, Nepal",
         "class":"Ten",
         "roll_no":"10",
         "dob":"1995-04-10"
      },
      {
         "student_id":"2",
         "full_name":"John Karki",
         "address":"Pokhara, Nepal",
         "class":"Nine",
         "roll_no":"3",
         "dob":"1996-02-21"
      },
      {
         "student_id":"6",
         "full_name":"Karan Chaudhary",
         "address":"Dang, Nepal",
         "class":"Eight",
         "roll_no":"33",
         "dob":"1993-04-03"
      },
      {
         "student_id":"1",
         "full_name":"Krishna Chaudhary",
         "address":"Kathmandu, Nepal",
         "class":"Ten",
         "roll_no":"7",
         "dob":"1994-06-25"
      },
      {
         "student_id":"3",
         "full_name":"Rabi Shahi",
         "address":"Dhangadhi, Nepal",
         "class":"Ten",
         "roll_no":"5",
         "dob":"1994-02-25"
      }
   ]
}

In this way, you can generate valid nested JSON with PHP and MySQL.

No any Comments on this Article


Please Wait...