1

我有一个选择表中所有行的 php 文件,我想将结果内容存储在 www/ 目录中的文件中,就像我可以解析该 name.json 文件并使用 jackson 库在 Android 中显示其内容一样。这是我的 php 文件:

<?php header('Content-type: application/json');

/*
 * Following code will list all the surveys
 */

// array for JSON response
$response = array();


// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// get all surveys from surveys table
$result = mysql_query("SELECT * FROM surveys") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // surveys node
    $response["surveys"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $survey = array();
        $survey["id_survey"] = $row["id_survey"];
        $survey["question_survey"] = $row["question_survey"];
        $survey["answer_yes"] = $row["answer_yes"];
        $survey["answer_no"] = $row["answer_no"];




        // push single survey into final response array
        array_push($response["surveys"], $survey);
    }
    // success
    $response["success"] = 1;

    // echoing JSON response
    echo json_encode($response);
} else {
    // no surveys found
    $response["success"] = 0;
    $response["message"] = "No surveys found";

    // echo no users JSON
    echo json_encode($response);
}
?>

因此:php 文件 => 执行 => 将结果存储在文件中 => 解析该文件并将其显示在 Android 中。

4

1 回答 1

2

如果要将 JSON 写入文件,只需添加:

file_put_contents( "filename.json", json_encode($response) );

在您的旁边或代替您的echo;

于 2012-11-19T11:06:57.470 回答