-2

所以这是查看我的 json 文件的地方:http: //alyssayango.x10.mx/

这是我的 php 文件:

  <?php
include('connectdb.php');
$sql = "SELECT * FROM tblmovies ORDER BY _id";
$result = mysql_query($sql);    
if($result === FALSE) {
 die(mysql_error()); // TODO: better error handling
}
$set = array();
while($row1 = mysql_fetch_assoc($result)) {
    $set[] = $row1;
}
echo json_encode($set);

输出是:

[{"_id":"3","movie_name":"Despicable Me 2","movie_cinema_number":"CINEMA     1","movie_length":"1hr. 40mins.","movie_type":"GP","movie_schedules":"12:10 PM | 02:25 PM | 04:40 PM","movie_image_url":"http:\/\/i39.tinypic.com\/szizo4.jpg"},{"_id":"4","movie_name":"White House Down","movie_cinema_number":"CINEMA 2","movie_length":"2 hrs. 10 mins.","movie_type":"PG-13","movie_schedules":"12:30 PM | 03:20 PM | 06:10 PM","movie_image_url":"http:\/\/i39.tinypic.com\/vp9n9j.jpg"},{"_id":"5","movie_name":"My Lady Boss","movie_cinema_number":"CINEMA 3","movie_length":"1hr. 50 mins.","movie_type":"PG-13","movie_schedules":"01:00 PM | 03:30 PM | 06:00 PM","movie_image_url":"http:\/\/i44.tinypic.com\/2qlv08z.jpg"},{"_id":"6","movie_name":"Four Sisters And A Wedding","movie_cinema_number":"CINEMA 4","movie_length":"2 hrs. 5 mins. ","movie_type":"PG-13","movie_schedules":"12:30 PM | 03:10 PM | 05:50 PM","movie_image_url":"http:\/\/i44.tinypic.com\/9iv0d1.jpg"}]

我在这里做的似乎有什么问题?URL 显示为:http:\ /\ /i44.tinypic.com\ /9iv0d1.jpg 其中应该是http://i44.tinypic.com/9iv0d1.jpg

4

2 回答 2

3

JSON 格式通常用于通过网络连接序列化和传输结构化数据。它主要用于在服务器和 Web 应用程序之间传输数据,作为 XML 的替代品。

如果您创建的API应该是:

$array = array("title" => "TEST", "username" => "test"); // Creating a array
echo json_encode($array); // Printing json

客户想要请求并得到响应:

$json = file_get_contents('http://www.example.com/test/films.json'); // Your url
$array = json_decode($json); // Your first array its here!

更多: http ://en.wikipedia.org/wiki/JSON
警告:您不能编辑或整理您的 json 响应!这很好!

于 2013-07-25T08:05:47.793 回答
0

问题不存在,其实。

您做错的是您使用了一段 JSON 字符串而没有事先对其进行解码。json_decode(..)如果您使用 PHP 或等效的 javascript 函数来解码 JSON,只需使用它。

完成此操作后,您将拥有一个对象/数组,其中包含正确格式的数据。

于 2013-07-25T07:49:06.843 回答