将返回的JSON数据解码为对象时出现问题

时间:2013-12-26 01:49:34

标签: php arrays json

我不确定如何准确获取值(访问对象并在页面上显示它们,或存储为变量等等,只是我想从JSON获取这些ID(9473,5649,7953 .......)输出

添加我正在解码的网址,所以你可以看看它,因为我真的很困惑。我得到了未定义的strClass或非对象错误:/

源API网址为:

api.worldoftanks.eu/wot/account/tanks/?application_id=demo&fields=tank_id&account_id=503066565

从网址获取内容的功能:

function get_url_contents($url){
    $crl = curl_init();
    $timeout = 5;
    curl_setopt ($crl, CURLOPT_URL,$url);
    curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
    $ret = curl_exec($crl);
    curl_close($crl);
    return $ret;
}

这是我得到的JSON返回打印(1,2 ......... 89):

stdClass Object
(
    [status] => ok
    [count] => 1
    [data] => stdClass Object
        (
            [503066565] => Array
                (
                    [0] => stdClass Object
                        (
                            [tank_id] => 9473
                        )

                    [1] => stdClass Object
                        (
                            [tank_id] => 5649
                        )

                    [2] => stdClass Object
                        (
                            [tank_id] => 7953
                        )
                    [89] => stdClass Object
                        (
                            [tank_id] => 64817
                        )

                )

        )

)

这是原始的JSON输出(它的时间要长得多,但主要是相同的,只有更多的tank_ids):

{"status":"ok","count":1,"data":{"503066565":[{"tank_id":9473},{"tank_id":5649},{"tank_id":7953},{"tank_id":64817}]}}

我试图通过几种方式从中获取数据,但问题是,ID(503066565)是数组,每当我收到有关非对象数据或未定义属性的错误时:stdClass:

当没有数组时,我习惯从JSON获取数据,所以我现在有点困惑。

我也使用PHP。

评论代码是我尝试的事情的样本(我尝试了20种可能的选项,但我不经常使用JSON输出,所以我需要你的帮助。

$wg_id = "503066565";
$wot = json_decode(get_url_contents("URL"));
//$tank_id = $wot->data->$wg_id->in_garage;
/*
$tank_id = $wot->data->$wg_id['in_garage'];
foreach ($wot as $i){
    echo $i['tank_id'];
}
*/
echo $tank_id;

如何获取所有这些数据(可能是数组)?我需要他们在从DB中选择东西时比较这些ID。也可能有另一个值in_garage,是否可以选择将它添加到数组并知道哪些值在一起(你知道,每个in_garage都是针对特定的tank_id)。

5 个答案:

答案 0 :(得分:2)

将true作为json_decode的第二个参数传递,并将这些对象转换为数组。

答案 1 :(得分:1)

$wg_id是一个变量,当您将其用作属性时,您需要执行以下操作:

 $wot->data->{$wg_id}

或者您可以通过将json_decode的第二个参数设置为true来将其解码为数组。

$wot = json_decode(get_url_contents("URL"), true);

然后结果将是一个数组。

你可以像$wot['data'][$wg_id]

那样访问它

答案 2 :(得分:0)

我相信这应该有效:

echo $wot->data->{$wg_id}[0]->tank_id;

或者您可以在$wot->data->{$wg_id}上进行迭代并访问$item->tank_id

答案 3 :(得分:0)

好像你有一个JSON对象,其中包含数组,其中包含对象。因此,根据您显示的结构,请尝试使用此功能。我还为json_decode添加了一个选项,将结果作为一个数组返回,这对于直接访问可能更容易理解。

// $json = '{"status":"ok","count":1,"data":{"503066565":[{"tank_id":9473},{"tank_id":5649},{"tank_id":7953},{"tank_id":7697},{"tank_id":2625},{"tank_id":2817},{"tank_id":1553},{"tank_id":8721},{"tank_id":5377},{"tank_id":3137},{"tank_id":8977},{"tank_id":3857},{"tank_id":17},{"tank_id":55297},{"tank_id":7185},{"tank_id":10497},{"tank_id":1041},{"tank_id":14145},{"tank_id":5185},{"tank_id":4929},{"tank_id":10817},{"tank_id":52737},{"tank_id":10513},{"tank_id":11777},{"tank_id":11521},{"tank_id":273},{"tank_id":849},{"tank_id":10769},{"tank_id":1809},{"tank_id":12049},{"tank_id":513},{"tank_id":12097},{"tank_id":10529},{"tank_id":11585},{"tank_id":2561},{"tank_id":6657},{"tank_id":4369},{"tank_id":16145},{"tank_id":6977},{"tank_id":545},{"tank_id":6673},{"tank_id":529},{"tank_id":1793},{"tank_id":5137},{"tank_id":6721},{"tank_id":3905},{"tank_id":12561},{"tank_id":51457},{"tank_id":11553},{"tank_id":11281},{"tank_id":4353},{"tank_id":10049},{"tank_id":11265},{"tank_id":11793},{"tank_id":1537},{"tank_id":6465},{"tank_id":2049},{"tank_id":16641},{"tank_id":1089},{"tank_id":2113},{"tank_id":4625},{"tank_id":1},{"tank_id":54545},{"tank_id":51489},{"tank_id":9793},{"tank_id":11537},{"tank_id":2897},{"tank_id":1825},{"tank_id":2881},{"tank_id":6401},{"tank_id":289},{"tank_id":7761},{"tank_id":2385},{"tank_id":53537},{"tank_id":769},{"tank_id":51713},{"tank_id":5393},{"tank_id":1025},{"tank_id":3329},{"tank_id":6177},{"tank_id":3073},{"tank_id":785},{"tank_id":3089},{"tank_id":3105},{"tank_id":5153},{"tank_id":81},{"tank_id":51985},{"tank_id":609},{"tank_id":56577},{"tank_id":64817}]}}';
$url='http://api.worldoftanks.eu/wot/account/tanks/?application_id=demo&fields=tank_id&account_id=503066565';
$json = file_get_contents($url);
$wot_object = json_decode($json);
$wg_id = "503066565";

// This rolls through them as objects.
echo '<b>Object Access</b><br />';
foreach ($wot_object->data->$wg_id as $key => $value) {
  echo $key . ' | ' . $value->tank_id . '<br />';
}
echo '<br />';

// Or add the 'true' option to 'json_decode()' to return an array.
$wot_array = json_decode($json, true);
$wg_id = "503066565";

echo '<b>Array Access</b><br />';
foreach ($wot_array['data'][$wg_id] as $key => $value) {
  echo $key . ' | ' . $value['tank_id'] . '<br />';
}
echo '<br />';

echo '<b>Direct Array Access</b><br />';
// Which allows for simpler direct access like this.
echo $wot_array['data'][$wg_id][0]['tank_id'] . '<br />';
echo $wot_array['data'][$wg_id][1]['tank_id'] . '<br />';
echo $wot_array['data'][$wg_id][2]['tank_id'] . '<br />';
echo $wot_array['data'][$wg_id][89]['tank_id'] . '<br />';
echo '<br />';

答案 4 :(得分:0)

在尝试了JakeGoulds的回答之后,我查看了我的代码,找出他工作的原因,而不是我的。 我发现,我忘了包含Curl.class.php文件:D 因此,您可以将我的代码用于函数,可以获取所有内容,或者使用Jakes。但如果你使用我的,不要忘记在那里包含Curl.class.php文件,否则它不会工作。

抱歉愚蠢的问题,至少答案是:)