Asked  7 Months ago    Answers:  5   Viewed   24 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

74

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

Wednesday, March 31, 2021
 
Axalix
answered 7 Months ago
19

What a HORRENDOUS debug session.. well there's good news.. I figured it out..

I started looking at it using AJAX and logging it with Firebug... and it turns out json_decode (or eval by the way) cannot handle ", which is what PHPUnit sends back (Come on Sebastian!), so to fix it:

$json = str_replace('"', '"', $json);

Now I thought they were the same.. maybe someone can enlighten me..

Wednesday, March 31, 2021
 
WooDzu
answered 7 Months ago
58

I answered a similar question here: Sencha seems to not like Rails' json. It is specific to Rails but the concept still applies.

Essentially James Pearce is correct. What you are returning needs to be wrapped in a tag and the callback function. This will insert the code on your page and run the script, which has the effect of calling the function you provide.

$response = "<script type='text/javascript'>";
$response .= $_GET['callback'] . "(" . json_encode($row) . ")";
$response .= "</script>";
$this->output->set_content_type('application/json')->set_output($response);
Wednesday, March 31, 2021
 
GGio
answered 7 Months ago
87

jq 1.5 has a streaming parser. The jq FAQ gives an example of how to convert a top-level array of JSON objects into a stream of its elements:

$ jq -nc --stream 'fromstream(1|truncate_stream(inputs))'
[{"foo":"bar"},{"foo":"baz"}]
{"foo":"bar"}
{"foo":"baz"}

That may be enough for your purposes, but it is worthwhile noting that setpath/2 can be helpful. Here's how to produce a stream of leaflets:

jq -c --stream '. as $in | select(length == 2) | {}|setpath($in[0]; $in[1])'

Further information and documentation is available in the jq manual: https://stedolan.github.io/jq/manual/#Streaming

Thursday, July 29, 2021
 
aWebDeveloper
answered 3 Months ago
77
function recursive_array_intersect_key(array $array1, array $array2) {
    $array1 = array_intersect_key($array1, $array2);
    foreach ($array1 as $key => &$value) {
        if (is_array($value) && is_array($array2[$key])) {
            $value = recursive_array_intersect_key($value, $array2[$key]);
        }
    }
    return $array1;
}

Demo here.

Friday, August 13, 2021
 
rory
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :