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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?