1

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?

code
  • 69
  • 1
  • 9
  • 1
    please update your question with your exact json string, what you have now is invalid json, you can verify it at jsonlint.com – Alex Andrei Jan 05 '16 at 07:41
  • Could you please try doing the following: `$obj = json_decode($variable, true);` and then trying to echo `$obj['results']['price_link/_text'];` – Matt Jan 05 '16 at 07:42
  • Is the usage of `/` in the properties name is something you can change? I'm not sure if PHP supports the usage of that character in properties names. – Ofir Baruch Jan 05 '16 at 07:50
  • I have updated with the location from where the json is coming from – code Jan 05 '16 at 07:53
  • I am not able to change the /. It comes from www.import.io – code Jan 05 '16 at 07:53

1 Answers1

3

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).

Justinas
  • 41,402
  • 5
  • 66
  • 96