Receiving JSON Data - PHP

Asked

Viewed 223 times

0

good afternoon. I tried to do some research here, but I couldn’t find what I needed. I am working with JSON, and received the following return string:

{
    "success": 1,
    "return": {
        "100025362": {
            "pair": "BRL",
            "type": "sell",
            "amount": 21.615,
            "rate": 0.258,
            "timestamp_created": 1418654530,
            "status": 0
        },
        ...
    }
}

This number 100025362, is an open order that I own.

But at the time of this JSON consultation, I do not have the order number.

I would need to run all orders, to get the status.

If I had the order number, I’d be fine, I’d be:

$json_ActiveOrder = json_decode($stringJSON);
$ActiveOrderStatus = $json_ActiveOrder->return->100025362->status;

But as I do not have access to the number at this time, how would I run all orders? In case 100025362??

I tried to receive only until return, and loop, but I have the following errors with the code below:

$json_ActiveOrder = json_decode($retorno_ActiveOrder);
$ActiveOrder = $json_ActiveOrder->return;

foreach($ActiveOrder as $f) {
    echo $f[0];
}

The mistake comes out:

( ! ) Notice: Undefined property: stdClass::$return in C:\wamp\www\bitcoin\index.php on line 132
Call Stack
#   Time    Memory  Function    Location
1   0.0000  149328  {main}( )   ..\index.php:0

( ! ) Warning: Invalid argument supplied for foreach() in C:\wamp\www\bitcoin\index.php on line 134
Call Stack
#   Time    Memory  Function    Location
1   0.0000  149328  {main}( )   ..\index.php:0
  • you would just like to catch the number "100025362" ?

  • Yeah, I’d just like to take this number!!

  • have tried $x = json_decode($stringJSON, true); and then $x['return'][0] ?

2 answers

1

You can take the array key from the foreach:

foreach($ActiveOrder as $key => $f) {
    echo $key;
}

The first error that appears is that there is no property return and the second because the $ActiveOrder is not an array

  • So, but as I demonstrated, Return has in the json list yes. I’m going to do the test now, but would print in the case $key msm? or $f?

  • I tested it here, it didn’t work :( Gave the same errors. Remembering that I am setting $json_ActiveOrder = json_decode($retorno_ActiveOrder); $Activeorder = $json_ActiveOrder->Return;

  • print the $json_ActiveOrder variable

0

Do it like this, change the line:

$json_ActiveOrder = json_decode($retorno_ActiveOrder);

for:

$json_ActiveOrder = json_decode($retorno_ActiveOrder,true);

This will make the $json_ActiveOrder variable an Associative Array

If you want to look at the documentation: http://php.net/manual/en/function.json-decode.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.