2

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

6
  • this does not look like json to me... Commented Dec 26, 2013 at 1:51
  • looks like php array rather than json.. Is this $wot? if not can you print the value of $wot Commented Dec 26, 2013 at 1:55
  • I added raw JSON code. The first one is PHP function print_r(JSON_CODE) Commented Dec 26, 2013 at 1:56
  • I'm so confused with what you're asking here. You added the JSON that IS being output or that you WANT to output? Either way you're not doing that with objects. You need strings and arrays Commented Dec 26, 2013 at 1:59
  • $decodedObject -> data -> 503066565 [$someNumericIndex] -> tank_id. The question is not about JSON, but about how to access object properties. Commented Dec 26, 2013 at 1:59

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

 $wot->data->{$wg_id}

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]

1 Comment

Im using this code, to get data from JSON output and it works: code$battles = $wot->data->$wg_id->statistics->all->battles;code But in that code are no square brackets (arrays).
0

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 />';

3 Comments

Now I got 2 errors: Invalid argument supplied for foreach() and Undefined property: stdClass::$data
Hi, yes I tried converting it to array before I tought about writing there and problem is, that your code works, but well, only when I tried exactly your code. It doesnt work when I try it with my URL. I added the URL to question and I also added there the function to get contents of URL.
Ok, so you used file_get_contents instead of my function, when I changed that, now it works like a charm. So I dont have to use CURL to make it works, right? Anyway, really thank you.
0

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.

1 Comment

Its basicly same code I used, and as I told, I get this error with it: Undefined property: stdClass::$data
0

After trying Giacomo1968’s 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.

So you can use my code with function, that can get everything, or Giacomo’s 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!

Comments

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.