如何从aq PHP数据库生成JSON文件?

I have an online PHP/mySQL database from which I need an android app to be populated with data. How do I create a JSON file from the PHP/mySQL database?

Create an php file and access the database. With

json_encode($array)

you can produce a json representative from your array. Depending on your planned App you can just echo the json or use some kind of soap webservice

just select what you need from your DB, put it in $array and use

string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )

for example:

$json = json_encode($array)

and then put it in the file with:

int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )

example:

file_put_contents('file.json', $json)

Just use a php command to encode anything in json. Here is the php command which lets you to encode anything in json.

json_encode($text)

After encoding just put file_get_contents() function in php to put it in a .json file

In order to have JSON format of your data in the database:

  1. Fetch the data from the database using PHP and store them as an array called $data
  2. Convert the the array $data to JSON, use echo json_encode($data);to have them printed in JSON format