如何编码多行的JSON?

时间:2018-08-02 15:32:00

标签: php json

在开始之前,我已经浏览了多个平台上的其他示例和问答,但是似乎都无法解决我的问题。我正在尝试通过json从MySQL返回多行。但是,我一直做不到。下面的代码显示了我的尝试。

我通过邮递员得到我的回复。第一个while仅返回数据库中的最后一个条目,而do-while返回所有条目,但未正确编码json,因为json输出syntax error,但html部分显示了所有内容条目。

<?php
    $dashboard_content_token = $_REQUEST["dashboard_content_token"];
    $token = "g4";

    require(cc_scripts/connect.php);

    $sql = "SELECT * FROM `dashboard_content`";
    $check = strcmp("$token", "$dashboard_content_token");
    $statement = mysqli_query($con, $sql);
    if (check) {
        $rows = mysqli_fetch_assoc($statement);
        if (!$rows) {
            echo "No results!";
        } else {
              while ($rows = mysqli_fetch_assoc($statement)) {
                $news_id = $rows['news_id'];
                $image_url = $rows['image_url'];
                $news_title = $rows['news_title'];
                $news_description = $rows['news_description'];
                $news_article = $rows['news_article'];

                $result['dashboard content: '][] = array('news_id' => $news_id, 'image_url' => $image_url, 'news_title' => $news_title, 'news_description' => $news_description, 'news_article' => $news_article); 

                echo json_encode($result);
        }
        // do {
                // $news_id = $rows['news_id'];
                // $image_url = $rows['image_url'];
                // $news_title = $rows['news_title'];
                // $news_description = $rows['news_description'];
                // $news_article = $rows['news_article'];

                // $result['dashboard content: '][] = array('news_id' => $news_id, 'image_url' => $image_url, 'news_title' => $news_title, 'news_description' => $news_description, 'news_article' => $news_article); 

                // echo json_encode($result);
        //     } while ($rows = mysqli_fetch_assoc($statement));


        mysqli_free_result($statement);
    }
}
?>

2 个答案:

答案 0 :(得分:1)

在while循环中,

$result['dashboard content: '] = array('news_id' => $news_id, 'image_url' => $image_url, 'news_title' => $news_title, 'news_description' => $news_description, 'news_article' => $news_article); 
每次运行循环时,

都会在$ result数组中覆盖相同的“仪表盘内容”条目。这就是为什么您只看到最后一个条目的原因。

在循环中执行json_encode()也是没有意义的,因为您只会输出多个断开连接的单个JSON对象,这些对象不属于数组或连贯结构。这不会产生有效的JSON响应。

尚不清楚您到底希望什么输出结构,但这可能会为您提供解决方案,或者至少可以向正确的方向推:

$statement = mysqli_query($con, $sql);
$result = array("dashboard_content" => array()); //create an associative array with a property called "dashboard_content", which is an array. (json_encode will convert an associative array to a JSON object)

if (check) {
    $rows = mysqli_fetch_assoc($statement);
    if (!$rows) {
        echo "No results!";
    } else {
          while ($rows = mysqli_fetch_assoc($statement)) {
            $news_id = $rows['news_id'];
            $image_url = $rows['image_url'];
            $news_title = $rows['news_title'];
            $news_description = $rows['news_description'];
            $news_article = $rows['news_article'];

            //append the current data to a new entry in the "dashboard_content" array
            $result["dashboard_content"][] = array('news_id' => $news_id, 'image_url' => $image_url, 'news_title' => $news_title, 'news_description' => $news_description, 'news_article' => $news_article); 
        }
    }

    //now, output the whole completed result to one single, coherent, valid JSON array.
    echo json_encode($result);

您应该以这样的JSON结尾:

{
  "dashboard_content": [
    {
      "news_id": 1,
      "image_url": "abc",
      "news_title": "xyz",
      //...etc
    },
    {
      "news_id": 2,
      "image_url": "def",
      "news_title": "pqr",
      //...etc
    },
    //...etc
  ]
}

答案 1 :(得分:1)

这应该有效。您将要使用do...while语句,否则将跳过第一个结果。

<?php
    $dashboard_content_token = $_REQUEST["dashboard_content_token"];
    $token = "g4";

    require(cc_scripts/connect.php);

    $sql = "SELECT * FROM `dashboard_content`";
    $check = strcmp("$token", "$dashboard_content_token");
    $statement = mysqli_query($con, $sql);
    if (check) {
        $rows = mysqli_fetch_assoc($statement);
        if (!$rows) {
            echo "No results!";
        } else {

          do {
             $news_id = $rows['news_id'];
             $image_url = $rows['image_url'];
             $news_title = $rows['news_title'];
             $news_description = $rows['news_description'];
             $news_article = $rows['news_article'];

               $result['dashboard content: '][] = array('news_id' => $news_id, 'image_url' => $image_url, 'news_title' => $news_title, 'news_description' => $news_description, 'news_article' => $news_article); 


          } while ($rows = mysqli_fetch_assoc($statement));

        mysqli_free_result($statement);
        echo json_encode($result);
    }
}
?>

关键是将所有结果放入一个数组,然后只做一个json_encode()。当您多次调用json_encode()时,您的API将返回无效的json。