Viewed   61 times

This is the json that deepbit.net returns for my Bitcoin Miner worker. I'm trying to access the workers array and loop through to print the stats for my myemail@gmail.com worker. I can access the confirmed_reward, hashrate, ipa, and payout_history, but i'm having trouble formatting and outputting the workers array.

{
 "confirmed_reward":0.11895358,
 "hashrate":236.66666667,
 "ipa":true,
 "payout_history":0.6,
 "workers":
    {
      "myemail@gmail.com":
       {
         "alive":false,
         "shares":20044,
         "stales":51
       }
    }
}

Thank you for your help :)

 Answers

2

I assume you've decoded the string you gave with json_decode method, like...

$data = json_decode($json_string, TRUE);

To access the stats for the particular worker, just use...

$worker_stats = $data['workers']['myemail@gmail.com'];

To check whether it's alive, for example, you go with...

$is_alive = $worker_stats['alive'];

It's really that simple. )

Saturday, September 10, 2022
 
5

Try something like this:

//initialize array
$myArray = array();

//set up the nested associative arrays using literal array notation
$firstArray = array("id" => 1, "data" => 45);
$secondArray = array("id" => 3, "data" => 54);

//push items onto main array with bracket notation (this will result in numbered indexes)
$myArray[] = $firstArray;
$myArray[] = $secondArray;

//convert to json
$json = json_encode($myArray);
Friday, December 23, 2022
4

Your $data_drugs is no longer a json, after json_decode is an associative array.
You don't need any loop to see keys and values

$data_drugs = json_decode($json_drugs,true);
print_r($data_drugs);

/* or if you don't like inline */

echo'<pre>';
print_r($data_drugs);
echo'</pre>';

You can use var_dump($data_drugs) - keys and values with types, probably you don't need this
But if you want to display keys and values more ...fancy use a recursive function

function show($x){
    foreach($x as $key=>$val){
        echo"<p>$key : ";
        if(is_array($val)){ echo". . ."; show($val);}
        else{ echo"$val</p>";}}}

show($data_drugs);
Saturday, October 15, 2022
 
4

you can use it like this, in JSON format when you evaluate false value it will give you blank, and when you evaluate true it will give you 1.

$str = '[{"clientId":"17295c59-4373-655a-1141-994aec1779dc","channel":"/meta/connect","connectionType":"long-polling","ext":{"fm.ack":false,"fm.sessionId":"22b0bdcf-4a35-62fc-3764-db4caeece44b"},"id":"5"}]';

$arr = json_decode($str,true);

if($arr[0]['ext']['fm.ack'])    // suggested by **mario**
{
    echo "true";    
}
else {
    echo "false";   
}
Thursday, October 6, 2022
1

If the output of value.producer is an object, then you should be able to access the properties of that object. For example, value.producer.name should output 'asdfsadf'.

UPDATE

It looks as though your json data is not valid. (I'm assuming "producers" is a property of the data object and the value of that property is an array of all of the "producer" objects.)

I've created this fiddle ( http://jsfiddle.net/kAsPm/ ) and it works as expected. I just had to change the comma to a colon after "producers":

"producers":  // changed from "producers",
[
    {
        "producer":{
            "id":"1",
            ...
Sunday, October 30, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :