0

Not sure what to do, I have tried everything I can think of and search up. Now I don't know much about json, so I could be missing something. I am trying to read this multilevel json data.

{
  "status" : "success",
  "data" : {
    "network" : "BTCTEST",
    "txs" : [
      {
        "txid" : "13641a6bd0d0f9c166756bcf37d4f1d0bb435eba7803233b14e8d9aa1f58395d",
        "from_green_address" : true,
        "time" : 1499901470,
        "confirmations" : 5,
        "amounts_received" : [
          {
            "recipient" : "2Mxwy1ZUPJMcpiPath6HVzguyXdt6cMKmuQ",
            "amount" : "0.00080000"
          }
        ],
        "senders" : [
          "2N9fTBM2CmC6kyLHdqk8UwUvaz1DBZRQYcX"
        ],
        "confidence" : 1.0,
        "propagated_by_nodes" : null
      }
    ]
  }
}

I am trying to get the data from data>txs>amounts_received>amount, but it keeps coming up as NULL. I can't seem to find anyway to fix this, but currently my code looks like this:

var_dump($newAddressInfo->data->txs[0]->amounts_received->amount);

Can anyone help with this issue? Thanks!

TymeBomb
  • 117
  • 2
  • 12
  • show your code ... fyi , json IS NOT an array, it is a string representation of some underlying data structure. You must first convert the string json to a usable form , in any language. – YvesLeBorg Jul 13 '17 at 00:28
  • @YvesLeBorg I did the 1 line of code is the code. I used json_decode but never saw that it changed anything, am I wrong? – TymeBomb Jul 13 '17 at 00:33
  • Possible duplicate of [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – user3942918 Jul 13 '17 at 02:49

1 Answers1

1

You have this part in your JSON :

"amounts_received" : [
  {
    "recipient" : "2Mxwy1ZUPJMcpiPath6HVzguyXdt6cMKmuQ",
    "amount" : "0.00080000"
  }
],

So you have a list of objects.

But the corresponding part of your access is :

amounts_received->amount

You should use something like

amounts_received[0]->amount

Or remove the [] brackets in the JSON and not have a list.

Seblor
  • 6,947
  • 1
  • 25
  • 46
  • The JSON is read from a site's API so the format can't be changed. I need some way to read the whole json and just get the "amount" part in "amounts_received". I tried with just what you wrote and still nothing, anything else I can try? – TymeBomb Jul 13 '17 at 01:08
  • What do you mean "still nothing" ? What is printed if you use `var_dump($newAddressInfo->data->txs[0]->amounts_received[0]->amount` ? – Seblor Jul 13 '17 at 01:10
  • 1
    Well for some reason I ran that exact code before, but it never worked. That did work though, it got what I wanted! Thanks! Guess I just overlooked a step. – TymeBomb Jul 13 '17 at 01:14