试图从json对象获取值[重复]
问题描述:
This question already has an answer here:
I have this JSON: Full json coming from here. I tried to follow this but I am having a hard time doing so
results: [
{
marketcap_price/_currency: "USD",
available_link_numbers/_source: "15,045,750",
marketcap_price/_source: "$ 6,509,092,365",
number: 1,
name_link/_text: "Bitcoin",
name_link/_source: "/currencies/bitcoin/",
number/_source: "1",
price_link/_source: "/currencies/bitcoin/#markets",
available_link: "http://blockchain.info",
name_image/_source: "/static/img/coins/16x16/bitcoin.png",
price_link/_text: "$ 432.62",
pricegraph7d_link/_source: "/currencies/bitcoin/#charts",
price_link: "http://coinmarketcap.com/currencies/bitcoin/#markets",
pricegraph7d_image: "https://files.coinmarketcap.com/generated/sparklines/1.png",
volume24h_link/_source: "/currencies/bitcoin/#markets",
change24h_value: "0.39 %"
}
]
When I do
echo $obj->results[0]->number;
it outputs the value
1
BUT when I try to do
echo $obj->results[0]->price_link/_text;
It gives me nothing. I am using PHP for this. It should output
$432.62
Any way for me to get that price value? What am I doing wrong? Also, is it possible to get the value with just the numbers and no '$' symbol?
</div>
答
price_link/_text
is not valid PHP variable name. You have to use one of following approach:
Use {}
for custom name: $obj->results[0]->{'price_link/_text'}
Access data as array: $obj['results'][0]['price_link/_text']
(use json_decode($json, true)
to get information as array).