如何以php中的嵌套JSON对象为目标,并对其进行响应?
好吧,我想你只要看代码就知道我想做什么了如何以php中的嵌套JSON对象为目标,并对其进行响应?,php,json,Php,Json,好吧,我想你只要看代码就知道我想做什么了 //Get JSON text file (Steam API) $json = file_get_contents('http://store.steampowered.com/api/appdetails?appids=57690'); //Decode JSON $game_json = json_decode($json, true); //Target game name and echo it echo $game_json['name'
//Get JSON text file (Steam API)
$json = file_get_contents('http://store.steampowered.com/api/appdetails?appids=57690');
//Decode JSON
$game_json = json_decode($json, true);
//Target game name and echo it
echo $game_json['name'];
JSON本身的顺序如下(非结构化,非常抱歉):
所以我的目标是“name”:“Tropico 4:Steam特别版”
,这就是我想在我的页面上回应的内容。我不确定它是否有用,但“name”:
只出现一次,我的代码中是否需要类似于[0]
的内容来定位第一个?是嵌套阻止了我,还是$game_json['name']代码>不正确的目标定位方式
任何提示和/或帮助都将不胜感激。谢谢 以后,使用print\r($game\u json)
检查数组
结构
<?php
$json = file_get_contents('http://store.steampowered.com/api/appdetails?appids=57690');
$game_json = json_decode($json, true);
echo $game_json['57690']['data']['name'];
//Tropico 4: Steam Special Edition
echo $game_json['57690']['data']['required_age'];
//0
//etc...
以后,使用print\r($game\u json)
检查数组
结构
<?php
$json = file_get_contents('http://store.steampowered.com/api/appdetails?appids=57690');
$game_json = json_decode($json, true);
echo $game_json['57690']['data']['name'];
//Tropico 4: Steam Special Edition
echo $game_json['57690']['data']['required_age'];
//0
//etc...
可能的重复非常感谢。你太好了。这有点新,即使是最简单的教程(例如复制标记)也太令人困惑了。非常欢迎@JohnSmith。你会成功的!坚持是关键:)如果我的答案对你有帮助,请记为正确答案,谢谢!当然,但不能马上做,等待计时器@约翰史密斯将此作为答案。当你在评论部分提出完全无关的随机问题时,不要把他当作人质。非常感谢。你太好了。这有点新,即使是最简单的教程(例如复制标记)也太令人困惑了。非常欢迎@JohnSmith。你会成功的!坚持是关键:)如果我的答案对你有帮助,请记为正确答案,谢谢!当然,但不能马上做,等待计时器@约翰史密斯将此作为答案。当你在评论部分提出完全无关的随机问题时,不要把他当作人质。谢谢你,我想我现在明白了!当我输入时,我可能会问,如何构造这个API以使其可读?这只是浏览器中的一长串文本。根据您的编辑器,您可能有内置功能或插件可用于自动格式化JSON。我使用记事本+,有一个叫做“JSON查看器”的插件,可以为我自动格式化。很好,正是我想要的。这个社区太友好了。谢谢,谢谢,我想我现在明白了!当我输入时,我可能会问,如何构造这个API以使其可读?这只是浏览器中的一长串文本。根据您的编辑器,您可能有内置功能或插件可用于自动格式化JSON。我使用记事本+,有一个叫做“JSON查看器”的插件,可以为我自动格式化。很好,正是我想要的。这个社区太友好了。谢谢
<?php
//This is your json string
$string = {"57690":{"success":true,"data":{"type":"game","name":"Tropico 4: Steam Special Edition"...
//Turn JSON string into object
$data = json_decode($string);
//Turn your object into an array (easier to work with in this case)
$data = (Array)$data;
//Get name of item with "57690" key
$name = $data["57690"]->data->name;
//Echo the name
echo($name);
//You can also echo out all names of all items like this:
foreach($data as $key => $item)
{
echo($item->data->name);
}