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 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?