PHP解码JSON [英] PHP decoding json

查看:254
本文介绍了PHP解码JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在这里谁能帮我用PHP的JSON解码?我试着去解code一个JSON API URL

could anyone here help me with php an decoding json? Im trying to decode a json api url

下面是我的时刻:

  $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;

和这个打印出someusername,但我不能把它打印出来的工人当我尝试:

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.

运行Server 2008 R2使用PHP 5.3和IIS 7.5

running Server 2008 R2 with php 5.3 and IIS 7.5

推荐答案

您可以使用大括号语法通过的浓汤

You can use the curly brackets syntax suggested by Gumbo:

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

不过,最好的办法(IMO,为了一致性)是使用生成的对象作为关联数组:

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

这篇关于PHP解码JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆