Json php decoding

Can anyone here help me with php decrypt json? Im trying to decrypt json api url

Here is what I have at the moment:

$string = ' { "username": "someusername", "unconfirmed_reward": "0.08681793", "send_threshold": "0.01000000", "confirmed_reward": "0.02511418", "workers": { "bitcoinjol.jason-laptop": {"last_share": 1307389634, "score": "0", "hashrate": 0, "shares": 0, "alive": false}, "bitcoinjol.david-laptop": {"last_share": 1307443495, "score": "1.7742", "hashrate": 24, "shares": 1, "alive": true}, "bitcoinjol.pierre-pc": {"last_share": 1307441804, "score": "0", "hashrate": 0, "shares": 0, "alive": true}, "bitcoinjol.testJol": {"last_share": 0, "score": "0", "hashrate": 0, "shares": 0, "alive": false} }, "wallet": "asdasdjsadajdasjdsajasjdajdajs", "estimated_reward": "0.00131061" }'; $json_o = json_decode($string); echo $json_o->username; 

and this prints "someusername", but I can't get it to print workers when I try:

 echo $json_o->workers->someusername.jason-laptop; 

I think that "." or are the "-" that I use invalid?

I would like to be able to print each employee, and then the stacker, username and remuneration, etc. using arrays or these objects, anyway. I also tried splitting $ String into "," with to explode, but can't make it work well.

Server 2008 R2 is running with php 5.3 and IIS 7.5

+1
source share
5 answers

You can use the curly bracket syntax suggested by Gumbo :

 $json_o->workers->{"someusername.jason-laptop"} 

However, the best way (imo, for consistency) is to use the resulting object as an associative array:

 $object = json_decode($string, true); $object['workers']['bitcoinjol.jason-laptop']['last_share']; // 1307389634 
+5
source

The curly brace syntax should work:

 $json_o->workers->{"someusername.jason-laptop"} 
+6
source
  $json_o = json_decode($string); print_r( $json_o->workers->{"bitcoinjol.jason-laptop"} ); 
+2
source

This should work:

 $json_o->workers['someusername.jason-laptop']; 
+1
source

- or . are not valid object property names. Instead, use json_decode($string, true) (true means "decode as an associative array"), and then run $json_o['workers']['someusername.jason-laptop'] .

+1
source

Source: https://habr.com/ru/post/890246/


All Articles