对不起,我很擅长学习PHP。我试图找到这个话题,但没有得到答案,希望有人可以帮助我。
我尝试从Rottentomatoes的API Json中显示,如果Rottentomatoes Json输出:
{"total":109,"reviews":[{"critic":"Andrea Chase","date":"2014-07-25","original_score":"2/5","freshness":"rotten","publication":"Killer Movie Reviews","quote":"succeeds only as an advertisement for the tablet mentioned every dozen words or so, and that forms the McGuffin chased with sadly ineffectual diligence by Segal and Diaz.","links":{"review":"hxxp://www.killermoviereviews.com/main.php?nextlink=display&dId=1818&subLinks="}}]
我如何在php中显示“Andrea Chase”值?
现在我怀疑从“总”值显示“109”,这是我的代码
e$url1 = 'hxxp://my-url-api-link';
$data = json_decode(file_get_contents($url1));
$total = $data->total;
echo $total;
答案 0 :(得分:1)
为了这个问题,我将你的JSON
放入变量:
$json = '{"total":109,"reviews":[{"critic":"Andrea Chase","date":"2014-07-25","original_score":"2/5","freshness":"rotten","publication":"Killer Movie Reviews","quote":"succeeds only as an advertisement for the tablet mentioned every dozen words or so, and that forms the McGuffin chased with sadly ineffectual diligence by Segal and Diaz.","links":{"review":"hxxp://www.killermoviereviews.com/main.php?nextlink=display&dId=1818&subLinks="}}]}';
现在,如果你这样做:
echo "<pre>";
$data = json_decode($json);
print_r($data);
您将获得持有Standard Object
的{{1}} PHP
:
JSON
根据此结构,您可以通过以下方式获取stdClass Object
(
[total] => 109
[reviews] => Array
(
[0] => stdClass Object
(
[critic] => Andrea Chase
[date] => 2014-07-25
[original_score] => 2/5
[freshness] => rotten
[publication] => Killer Movie Reviews
[quote] => succeeds only as an advertisement for the tablet mentioned every dozen words or so, and that forms the McGuffin chased with sadly ineffectual diligence by Segal and Diaz.
[links] => stdClass Object
(
[review] => hxxp://www.killermoviereviews.com/main.php?nextlink=display&dId=1818&subLinks=
)
)
)
)
:
Andrea Chase
这是如何工作的:
data是一个带有名为review的属性的对象 - 所以echo $data->reviews[0]->critic;
现在评论是一个数组,我们需要它的第一个索引 - 所以$data->reviews
第一个索引又是一个名为critic的属性的对象。所以基于前面的解释:$data->reviews[0]
要记住的基本想法是$data->reviews[0]->critic
被访问为Array indexes
等。
$array[0], $array[1]
访问Object properties
,$object->property1
等。