我已经得到了这个代码:
$url = "http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=".$name;
$url = file_get_contents($url);
echo $url;
这个的输出是
{
"success":true,
"lowest_price":"4,20u20ac",
"volume":"4,855",
"median_price":"4,16u20ac"
}
,我只想要lowest_price
。我如何选择它?
您得到的输出称为JSON String。您可以使用json_decode()将其解析为数组。
<?php
$JSON_STRING="{"success":true,"lowest_price":"4,20u20ac","volume":"4,855","median_price":"4,16u20ac"}";
$array=json_decode($JSON_STRING,true);
以上代码将Json字符串转换为数组,您可以像这样访问lowest_price
(就像访问数组中的任何值一样),
<?php
echo $array["lowest_price"];
json_decode()中的第二个参数表示将JSON字符串转换为Array,默认返回PHP Object。
参考:http://php.net/manual/en/function.json-decode.php
Try
$url = "http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=".$name;
$url = file_get_contents($url);
echo $url;
$data = json_decode($url, true);
$lowest_price = $data['lowest_price'];
echo $lowest_price;
我是这样做的:
$url = "http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=".$name;
$data = file_get_contents($url);
$json = json_decode($data);
$lowest_price = $json->{'lowest_price'};
echo $lowest_price;