我对json和php有疑问。我想在wins_ratio中获取值。我怎么做到的?
<?php
$jsondata = file_get_contents('https://api.worldoftanks.eu/wot/account/info/?application_id=ea6d19fe37d4b6b660f18c322a6ec219&account_id=' .$acc_account_id );
$info = json_decode($jsondata, TRUE);
$account = $info['data']['wins_ratio'][0];
foreach ($account as $acc) {
$acc_value = $acc['value'];
echo $acc_value;
}
?>
&#13;
JSON文件
PHP文件
答案 0 :(得分:0)
根据JSON判断,它应该是
$account = $info['data']['521997295']['wins_ratio']
现在你将拥有帐号:
数组{ “rank_delta”:4690, “等级”:278273, “价值”:51.67 }
答案 1 :(得分:0)
解码JSON数据,然后深入查看 wins_ratio 属性。就是这样。 这里:
JSON编码数据
<?php
$json = '{
"status": "ok",
"meta": {
"count": 1
},
"data": {
"521997295": {
"survived_ratio": {
"rank_delta": 3365,
"rank": 1472735,
"value": 20.44
},
"capture_points": {
"rank_delta": null,
"rank": null,
"value": null
},
"wins_ratio": {
"rank_delta": 4690,
"rank": 278273,
"value": 51.67
},
"spotted_count": {
"rank_delta": null,
"rank": null,
"value": null
},
"account_id": 521997295,
"frags_avg": {
"rank_delta": null,
"rank": null,
"value": 0.74
},
"hits_ratio": {
"rank_delta": -1070,
"rank": 1222866,
"value": 52.89
},
"xp_amount": {
"rank_delta": null,
"rank": null,
"value": 2508278
},
"frags_count": {
"rank_delta": 822,
"rank": 677480,
"value": 5188
},
"spotted_avg": {
"rank_delta": null,
"rank": null,
"value": null
},
"battles_to_play": 0,
"damage_dealt": {
"rank_delta": 685,
"rank": 752542,
"value": 3573561
},
"global_rating": {
"rank_delta": 2563,
"rank": 542926,
"value": 4648
},
"xp_max": {
"rank_delta": -167,
"rank": 996086,
"value": 1773
},
"damage_avg": {
"rank_delta": null,
"rank": null,
"value": 509.49
},
"xp_avg": {
"rank_delta": 738,
"rank": 777970,
"value": 357.61
},
"battles_count": {
"rank_delta": 516,
"rank": 743475,
"value": 7014
}
}
}
}';
PHP工作
<?php
// DECODE THE JSON DATA...
$decoded = json_decode($json);
$data = $decoded->data;
$winsRatio = "";
// LOOP THROUGH THE DECODED DATA AND CHECK IF wins_ratio EXISTS
// AS A PROPERTY. IF IT DOES, ASSIGN IT TO THE $winsRatio Variable
// AND BREAK OUT OF THE LOOP: NO NEED TO CONTINUE LOOPING...
foreach($data as $key=>$obj){
if(property_exists($obj, 'wins_ratio')){
$winsRatio = $obj->wins_ratio;
break;
}
}
var_dump($winsRatio);
<强>转储强>
object(stdClass)[6]
public 'rank_delta' => int 4690
public 'rank' => int 278273
public 'value' => float 51.67
现在你可以像这样访问胜利属性比率:
<?php
$rankDelta = $winsRatio->rank_delta;
$rank = $winsRatio->rank;
$value = $winsRatio->value;
希望这有点帮助; - )
答案 2 :(得分:0)
使用此代码:将$info['data']['wins_ratio'][0]
替换为$info['data']['521997295']['wins_ratio']
,将$acc['value']
替换为foreach循环中的$acc
<?php
$jsondata = file_get_contents('https://api.worldoftanks.eu/wot/account/info/?application_id=demo&account_id=' .$acc_account_id );
$info = json_decode($jsondata, TRUE);
$account = $info['data']['521997295']['wins_ratio'];
foreach ($account as $acc)
{
echo $acc;
echo "<br>";
}
?>
要显示等级值使用,则不需要foreach循环
echo $info['data']['521997295']['wins_ratio']['value']; ///51.67
这会给你:
4690
278273
51.67
现场示例CLICK HERE
答案 3 :(得分:0)
您在代码中缺少父层次结构( 521997295 )。我还怀疑父级层次结构是$ acc_account_id变量,因此为了使其对您的内容具有动态性,您只需将代码调整为以下内容:
$jsondata = file_get_contents('https://api.worldoftanks.eu/wot/account/info/?application_id=ea6d19fe37d4b6b660f18c322a6ec219&account_id=' .$acc_account_id );
$info = json_decode($jsondata, TRUE);
$account = $info['data'][$acc_account_id]['wins_ratio'];
foreach ($account as $acc) {
$acc_value = $acc['value'];
echo $acc_value;
}
甚至更简单:
$jsondata = file_get_contents('https://api.worldoftanks.eu/wot/account/info/?application_id=ea6d19fe37d4b6b660f18c322a6ec219&account_id=' .$acc_account_id );
$info = json_decode($jsondata, TRUE);
$account = $info['data'][$acc_account_id]['wins_ratio']['value'];
如果您希望静态分配仅适用于您提供的案例,则可以将“$ acc_account_id”替换为“'521997295'”(包括密钥周围的两个单引号)。
如果您需要任何澄清,请告诉我。