使用PHP简单地创建JSON

时间:2015-04-03 10:12:35

标签: php json

PHP:$ result = mysql_query(“SELECT * FROM events LIMIT 15”);

任何人都可以帮忙并告诉我如何创建json,看起来与上面的php查询完全相同吗?

{
"events": [
    {
     "id": 2,
        "name": "TIME",
        "image": "http://127.0.0.1/android_tests_json_and_xml/images/time_best.jpg",
        "status": "30 years of Cirque du Soleil's best photos",
        "profilePic": "http://127.0.0.1/android_tests_json_and_xml/images/time.png",
        "timeStamp": "1403375851930",
        "url": "http://ti.me/1qW8MLB"
    },
    {
        "id": 3,
        "name": "Discovery",
        "image": "http://127.0.0.1/images/discovery_mos.jpg",
        "status": "A team of Austrian scientists has developed a laser system that causes fruit flies to dance.",
        "profilePic": "http://127.0.0.1/android_tests_json_and_xml/images/discovery.jpg",
        "timeStamp": "1403375851930",
        "url": "http://dsc.tv/xmMxD"
    },
    {
        "id": 11,
        "name": "A. R. rahman",
        "image": "http://127.0.0.1/images/ar_bw.jpg",
        "status": "",
        "profilePic": "http://127.0.0.1/android_tests_json_and_xml/images/ar.jpg",
        "timeStamp": "1403375851930",
        "url": ""
    }
]

}

我真的很感激

3 个答案:

答案 0 :(得分:2)

尝试这样,

$rows = array();
$result = mysql_query("SELECT * FROM events LIMIT 15");
while($row = mysql_fetch_assoc($result)) {
 $row['events'][] = $row;
}
echo  json_encode($rows);

答案 1 :(得分:1)

$sth = mysqli_query("SELECT ...");
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
 $rows[] = $r;
}
return json_encode($rows);

答案 2 :(得分:0)

尝试使用class和json序列化接口

class Event implements JsonSerializable
{
        var $id;
        var $name;
        var $image;
        var $status;
        var $profilePic;
        var $timeStamp;
        var $url;
        public function __construct($id , $name , $image , $status , $profilePic , $timeStamp , $url) {



            $this->id=$id 
            $this->name=$name 
            $this->image=$image 
            $this->status=$status 
            $this->profilePic=$profilePic;
            $this->timeStamp=$timeStamp; 
            $this->url=$url;
        }

        public function jsonSerialize() {

        return [
            'name' => $this->name,
            'message' => $this->message,
            'id'=> $this->id,
            'name'=>$this->name,
            'image'=>$this->image,
            'status'=>$this->status,
            'profilePic'=>$this->profilePic,
            'timeStamp'=>$this->timeStamp,
            'url'=>$this->url
         ];
    }
}


$events=array();

$result = $db->query("SELECT * FROM events LIMIT 15");

if ($result) {

        while($row = $result->fetch_assoc()) {

            array_push($events,new Event($row['id'] , $row['name'] , $row['image'] , $row['status'] , $row['profilePic'] , $row['timeStamp'] , $row['url']));
        }

}
else {

    $events=null;
}
$db->close();

if($events==null)
{
    echo "data not found";
}
else
{
    echo json_encode($events);
}