donglang5157 2015-04-03 10:12
浏览 153
已采纳

使用PHP简单地创建JSON

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

Can anyone help and show me how create json that looks exactly like the one below from the php query above?

{
"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": ""
    }
]

}

I'd really appreciate it

  • 写回答

3条回答 默认 最新

  • dreinuqm992401 2015-04-03 10:23
    关注

    try like this,

    $rows = array();
    $result = mysql_query("SELECT * FROM events LIMIT 15");
    while($row = mysql_fetch_assoc($result)) {
     $row['events'][] = $row;
    }
    echo  json_encode($rows);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan