duanlumei5941 2013-12-26 01:49
浏览 132
已采纳

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

Im not sure how to exactly get values (access objects and show them on page, or store as variables, etc. just I want to get those IDs (9473, 5649, 7953 .......) from JSON output.

Adding the URL which I am decoding, so you can take a look at it, cos Im really confused. Either I got undefined strClass or non-object error :/

The source API URL is:

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

Function to get content from URL:

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

This is print of JSON return I get (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
                        )

                )

        )

)

This is raw JSON output (its much longer, but its mainly same, just more tank_ids there):

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

I tried to get data from it in couple of ways, but problem is, that ID (503066565) is Array and every time I get error about non-object data or Undefined property: stdClass:

Im used to get data from JSON when there is no array, so Im little bit confused now.

Also I use PHP.

Commented code is sample of things I tried (and I tried like 20 possible options that came to my mind), but I dont work with JSON output often, so I need your help.

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

How can I get all those data out (maybe to array)? I need them to compare those IDs when selecting stuff from DB. Also there might be another value in_garage, is there option to add it to array and know which values are together (you know, each in_garage is for specific tank_id).

  • 写回答

5条回答 默认 最新

  • dongxun2089 2013-12-26 02:00
    关注

    Seems like you have a JSON object that has arrays in it with objects in them. So based on the structure you are showing try this instead. I also have added an option to json_decode to return the results as an array which might be easier to understand for direct access.

    // $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条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记