Jump to content

Parse JSON in PHP and retrieve object names.


Jriker1

Recommended Posts

I have some json that would like to parse thru without hardcoding the subvalues so others than me can use this as well. Example of the JSON is:

{

"payout_history":"0",

"round_shares":"1816",

"workers":

  {

    "jbo.5970":

      {

         "alive":"1",

         "hashrate":"1253"

      },

    "jbo.5970cpu":

      {

        "alive":"1",

        "hashrate":"21"

      },

    "jbo.5970-2":

      {

        "alive":"1",

        "hashrate":"1062"

      }

    }

}

 

So those jbo level items I can directly access by ['workers']['jbo.5970'] however tried like ['workers][0] but nothing was returned. Want to parse thru all the items under workers and then process the array elements in each jbo value.  The jbo.* values are just examples, they could be anything so not static. Thoughts?

 

UPDATED INFO:

 

Using the below, I can get the alive and hash rate status of each worker which I am surprised about as I thought it was one level deeper than I'm going to get them. But I can not get the name of the worker itself.  Basically want to output the worker name itself, then if alive and how fast it's hashrate is.

$wemineltc = file_get_contents("http://localhost/wemineltc.json");

$obj=json_decode($wemineltc,true);

foreach ($obj['workers'] as $wrk)

  {

    echo $wrk['alive'] . "<br>";

    echo $wrk['hashrate'] . "<br>";

  }

 

I can also do like $obj['workers']['jbo.5970']['alive'] to get status of a particular worker, however as mentioned I am assuming the workers name is dynamic.

 

Here is an example URL by the way: https://www.wemineltc.com/api?api_key=6fd24db2b31d3982ad5520c009588efe81b1b4cc07e9fcd7904d04434405e3ef

 

Thanks.

 

JR

As another follow-up what if it goes one level deeper? So want to output a table like:

 

LTC: jbod.5970: 20

LTC: jbod.5970cpu: 30

NVC: jbod.5970: 50

NVC: jbod.5970cpu: 0

NVC: jbod.5970two: 0

 

 

"workers":
 {
   "ltc":
     {
       "jbod.5970":
         {
           "hashrate":"20"
         },
        "jbod.5970cpu":
         {
           "hashrate":"30"
         },
      },
    "nvc":
      {
        "jbod.5970":
          {
            "hashrate":"50"
          },
        "jbod.5970cpu":
          {
            "hashrate":"0"
          },
        "jbod.5970two":
          {
            "hashrate":"0"
          },
       }
  }

 

 

Thanks.  Hoping this will give me that ahh ha moment and all will become clear.

 

Thanks.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.