[英]How do I target nested JSON objects in php, and echo them?
好的,所以我認為您只要看一下代碼就可以實現我想做的事情。
//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本身按此順序排列(非結構化,非常抱歉):
{"57690":{"success":true,"data":{"type":"game","name":"Tropico 4: Steam Special Edition"
所以我的目標是""name":"Tropico 4: Steam Special Edition""
,這是我想在頁面上回顯的內容。 我不確定是否有幫助,但是"name":
出現一次,我的代碼中是否需要類似[0]
代碼來定位第一個? 是嵌套阻止了我,還是$game_json['name'];
定位方式不正確?
任何提示和/或幫助將不勝感激。 謝謝。
將來,請使用print_r($game_json)
檢查array
結構。
<?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...
<?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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.