1

could anyone here help me with php an decoding json? Im trying to decode a 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 out "someusername" but I cant get it to print out the workers when I try:

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

I think that the "." or the "-" I am using are invalid?

I would like to be able to print out each worker and then the waller, username and rewards ect.. using arrays or these objects, either way. I have also tried splitting the $String on "," with explode, but cant get that to work nicely either.

running Server 2008 R2 with php 5.3 and IIS 7.5

1
  • Use print_r(); on arrays or objects to display detailed information. Echo works on strings only. Commented Jun 7, 2011 at 11:51

5 Answers 5

6

The curly braces syntax should work:

$json_o->workers->{"someusername.jason-laptop"}
Sign up to request clarification or add additional context in comments.

Comments

5

You can use the curly brackets 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

Comments

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

Comments

1

- or . are not valid object property names. Try instead using json_decode($string, true) (the true stands for "decode as an associative array"), and then do $json_o['workers']['someusername.jason-laptop'].

7 Comments

I do not find it a very elegant solution as "bitcoinjol.jason-laptop" is not a valid property name. It may work, but it's principally wrong.
It's not a may, it just works. So it's principally right. You might dislike to write properties in such a way, but as you can see, it's a valid property name for the json object that is available for access.
"just works" has nothing to do with "is right". For example $foo = 1; $$foo = 'blablabla'; echo $$foo; prints out 'blablabla' even though $1 is NOT a valid variable name. Also, quote from the php manual: "Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'"
@cypher: $1 is not, but $$foo is. A variable is a variable and a value is a value.
|
1

This should work:

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

2 Comments

Fatal error: Cannot use object of type stdClass as array in ...
You need to decode the json string into an array, then it works. see json_decode() parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.