简体   繁体   English

使用php从sql数据库创建json对象

[英]creating json object from sql database using php

I am trying to create a json object from my mysql database for a android project. 我试图从我的mysql数据库为一个android项目创建一个json对象。 I need an output something like this: 我需要这样的输出:

{
    "feed": [
        {
            "id": 1,
            "name": "National Geographic Channel",
            "image": "http://api.androidhive.info/feed/img/cosmos.jpg",
            "status": "\"Science is a beautiful and emotional human endeavor,\" says Brannon Braga, executive producer and director. \"And Cosmos is all about making science an experience.\"",
            "profilePic": "http://api.androidhive.info/feed/img/nat.jpg",
            "timeStamp": "1403375851930",
            "url": null
        },
        {
            "id": 2,
            "name": "TIME",
            "image": "http://api.androidhive.info/feed/img/time_best.jpg",
            "status": "30 years of Cirque du Soleil's best photos",
            "profilePic": "http://api.androidhive.info/feed/img/time.png",
            "timeStamp": "1403375851930",
            "url": "http://ti.me/1qW8MLB"
        }
]
}

But I am getting ouput something like this: 但是我得到的输出是这样的:

{"feed":[{"id":"0","name":"punith","image":"","status":"ucfyfcyfffffffffffffffffffffffffffffffff","profilePic":"http:\/\/api.androidhive.info\/feed\/img\/nat.jpg","timestamp":"1403375851930","url":""}]}

Everything is on a single line and the id attribute should not be quotes. 一切都在一行上,并且id属性不应该用引号引起来。 Is there anything I could do.This is my php file 有什么我可以做的吗?这是我的php文件

<?php
define('HOST','');
define('USER','');
define('PASS','');
define('DB','');

$con = mysqli_connect(HOST,USER,PASS,DB);

$sql = "select * from timeline";

$res = mysqli_query($con,$sql);

$result = array();

while($row = mysqli_fetch_array($res)){
array_push($result,
array('id'=>$row[0],
'name'=>$row[1],
'image'=>$row[2],
'status'=>$row[3],
'profilePic'=>$row[4],
'timestamp'=>$row[5],
'url'=>$row[6]
));
}

echo json_encode(array("feed"=>$result));

mysqli_close($con);

?>

And will it affect if the output is on a single line. 并且会影响输出是否在单行上。 The database contains exactly the same columns used as attributes. 数据库包含与用作属性的列完全相同的列。

Thanks in advance 提前致谢

Try encoding with JSON_PRETTY_PRINT 尝试使用JSON_PRETTY_PRINT进行编码

$json_string = json_encode($data, JSON_PRETTY_PRINT);

where data is your result array. data是您的结果数组。

in your case 在你的情况下

echo json_encode(array("feed"=>$result),JSON_PRETTY_PRINT);

refer this Tutorial from the official PHP docs . 请参阅官方PHP文档中的本教程

ID in quotes 引号中的ID

ID is in quotes because it is a string, not an integer. ID用引号引起来,因为它是一个字符串,而不是整数。 You can change that by changing this: 您可以通过更改以下内容来更改它:

array('id'=>$row[0]

to this: 对此:

array('id'=>intval($row[0])

"Pretty Printing" “漂亮的印刷”

Putting it on multiple lines will only affect readability but not how the data is computed - but you can prettify it: Pretty-Printing JSON with PHP 将其放在多行上只会影响可读性,而不会影响数据的计算方式-但您可以修饰它: 使用PHP漂亮打印JSON

$output = json_encode(array("feed"=>$result), JSON_PRETTY_PRINT);
echo $output;

与其他答案一样,包含json标头会更具有语义,我发现仅包含此标头也有助于json本身的外观,因此它的格式正确,而不是所有连续的字符串。

header('Content-Type: application/json');

For php>5.4 对于php> 5.4

$json=json_encode(array("feed"=>$result),JSON_PRETTY_PRINT);
header('Content-Type: application/json');
print_r($json);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM