duanqie8549 2015-09-03 15:12
浏览 73
已采纳

如何使用数据库值创建动态json列表/数组

I am trying to make a json list to be used for a practice app I am busy with. It is supposed to look like this

 {
    "title": "District 9",
    "image": "http://api.androidhive.info/json/movies/2.jpg",
    "rating": 8,
    "releaseYear": 2009,
    "genre": ["Action", "Sci-Fi", "Thriller"]
},<---comma is present here
{
    "title": "Transformers: Age of Extinction",
    "image": "http://api.androidhive.info/json/movies/3.jpg",
    "rating": 6.3,
    "releaseYear": 2014,
    "genre": ["Action", "Adventure", "Sci-Fi"]
}

and this is the code I am currently using

<?php
        require '../../scripts/connect.php';

        $y="";
        $book ="";
        if($result = $db->query("SELECT * FROM posts") or die ($db->error)){
      if($count = $result->num_rows) {
             while($row = $result->fetch_object()){


        $y .= json_encode(array(
            "title"=> $row->title,
            "image"=>"http://www.mywebsite.com/posts/img/" . $row->id. ".jpg",
            "rating"=> $row->rating,
            "releaseYear"=> $row->date_added,
            "genre"=> [$row->category, $row->subcategory]

    ), JSON_PRETTY_PRINT);


      }
    }
    }
    echo '[' .$y. ']';


    ?>

However I get this...whereby the individual arrays are not seperated by comma's

[
{ "title": "This is a test",
 "image": "http:\/\/www.mywebsite.com\/posts\/img\/1.jpg",
 "rating": 8.7,
 "releaseYear": "2015-09-02 02:19:10",
 "genre": [ "genre", "genre" ]
 }<---no comma here is the problem
{ 
"title": "This is another test",
"image": "http:\/\/www.mywebsite.com\/posts\/img\/1.jpg", 
"rating": 7.7,
"releaseYear": "2015-09-03 02:19:10", 
"genre": [ "genre", "genre" ] 
}
]

my question is how would you go about adding a comma to seperate each json object in this instance.Thanks

  • 写回答

1条回答 默认 最新

  • drgbpq5930 2015-09-03 15:20
    关注

    The only thing you actually json_encode() is single book arrays. Because you simply paste all these pieces of JSON behind each other, nothing adds the comma for you.

    In stead of adding comma's manually, you should put all the books in an array and json_encode() the entire books array.

    This makes sure you have correct JSON in the entire output.

    Example

    $books = []; // I renamed $y to $books, seems clearer.
    $book = ""; // This doesn't do anything remove it.
    if($result = $db->query("SELECT * FROM posts") or die ($db->error)){
        if($count = $result->num_rows) {
            while($row = $result->fetch_object()){
            $books[] = [
                    "title"       => $row->title,
                    "image"       =>"http://www.mywebsite.com/posts/img/" . $row->id. ".jpg",
                    "rating"      => $row->rating,
                    "releaseYear" => $row->date_added,
                    "genre"       => [$row->category, $row->subcategory]
                ];
            }
        }
    }
    echo json_encode($books, JSON_PRETTY_PRINT);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配