2013-12-26 01:49



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:


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);
    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):


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).

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dongxun2089 dongxun2089 8年前

    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}]}}';
    $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 />';
    点赞 评论 复制链接分享
  • dongqiong8021 dongqiong8021 8年前

    After trying JakeGoulds answer, I looked at my code to find out why his is working and mine not. I found, that I forgot to include Curl.class.php file :D So you can use my code with function, that can get everything, or Jakes one. But if youll use mine, dont forget to include Curl.class.php file there, or it wont work.

    Sorry for stupid question, at least its answered :)

    点赞 评论 复制链接分享
  • dqls67891 dqls67891 8年前

    Pass true as the second argument to json_decode and those objects will be converted to arrays.

    点赞 评论 复制链接分享
  • douhan8009 douhan8009 8年前

    $wg_id is a variable, when you use it as a property, you have to do with:


    Or you could decode it to array by setting the second parameter of json_decode to true.

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

    Then the result will be an array.

    you could access it by like $wot['data'][$wg_id]

    点赞 评论 复制链接分享
  • dongyuans61046 dongyuans61046 8年前

    I believe this should work:

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

    or you could iterate on $wot->data->{$wg_id} and access the $item->tank_id.

    点赞 评论 复制链接分享