Richie Rich
Richie Rich

Reputation: 29

json_decode Does Not Work Properly

I have an issue with decoding JSON data. Does somebody have any idea why the variable $clanid is not set?

This is the code:

$url = "http://185.112.249.77:9999/Api/Search?search=&level=1&min=1&max=50&points=48000";
$jsondata = file_get_contents($url);
$data = json_decode($jsondata, true);

foreach ($data->clanList as $clan) {

$clanid = $clan->id;
echo $clan->id;
}

Thanks in advance for your help.

Upvotes: 0

Views: 548

Answers (5)

Rohan Khude
Rohan Khude

Reputation: 4883

You are trying to retrieve as object. Its not possible because ur decoding second parameter says json output is in associative array. Follow the below code

   <?php
//get the result from the url by using file_get_contents or curl
    $jsondata = file_get_contents("http://185.112.249.77:9999/Api/Search?search=&level=1&min=1&max=50&points=48000");
//decode the json in associative array by putting true as second parameter
    $data = json_decode($jsondata, true);
//fixed array is chosen, clanList is fixed so stored clanList in $in
    $in=$data['clanList'];
//for each element of clanList as key=>value nothing but "element":"value"
//for subarray in clanList use another foreach
    foreach ($in as $key=>$value) {
//to fetch value of element for each key          
    $clanid = $in[$key]['id'];
    echo $clanid;
    }
    ?>

Upvotes: 1

Red Acid
Red Acid

Reputation: 217

first you should check if the json is returned and then extract it. the problem is when the json is decoded will become 0, 1, 2, 3, etc and you will be unable yo get the clanList. for this you need to use array_values

$url = "http://185.112.249.77:9999/Api/Search?search=&level=1&min=1&max=50&points=48000";
$jsondata = file_get_contents($url);
$data = json_decode($jsondata, true);
$get_indexes = array_values($data);

if ($jsondata) {
    foreach ($get_indexes as $clan) {
    $clanid = $data[$clan]['id'];
    echo $clanid;
    }   
} else {
exit("failed to load stream");
}

Upvotes: 0

developerjack
developerjack

Reputation: 1213

You've one immediate error and one potential error.

json_decode() with a second param true will return an associative array (if it can). Therefore your foreach (and other references) should use array indexes not object fields.

Adding true seems intentional so I'll assume you wished to use the data as an associative array and not as an object. You could of course just remove the true param.

Since you have an external data source you might still get errors. For example json_decode() could also return false on invalid JSON.

If you're on php 5.5 you can use json_last_error_msg to retrieve the message. Otherwise you can fall back to json_last_error.

The correct, (mostly) error proof code would look like:

$url = "http://185.112.249.77:9999/Api/Search?search=&level=1&min=1&max=50&points=48000";
$jsondata = file_get_contents($url);
$data = json_decode($jsondata, true);
if($data === false) { // check for JSON errors as well.
    die(json_last_error());
}

foreach ($data['clanList'] as $clan) { // use array syntax here.
    $clanid = $clan['id']; // Use array syntax here.
    echo $clanid;
}

edit: added note about also possibly removing true as per other suggestions

Upvotes: 0

Steve
Steve

Reputation: 20469

The second argument for json_decode takes a boolean. If set to true, if forces the output as an array. It defaults to false, which will decode to an object, which is what you require

$data = json_decode($jsondata); //removed boolean arg

Upvotes: 2

Slowmove
Slowmove

Reputation: 462

Since you are calling json_decode with true as second parameter, your json object is decodec into an associative array and not an object and therefore the foreach should be

foreach($data['clanList'] as $clan

Have a look at php manual

assoc

When TRUE, returned objects will be converted into associative arrays.

Upvotes: 2

Related Questions