Php json_解码无法正常工作
我在解码JSON数据时遇到了一个问题。 有人知道为什么没有设置变量Php json_解码无法正常工作,php,json,decode,Php,Json,Decode,我在解码JSON数据时遇到了一个问题。 有人知道为什么没有设置变量$clanid 代码如下: $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->cl
$clanid
代码如下:
$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;
}
提前感谢您的帮助。json_decode的第二个参数采用布尔值。如果设置为true,则If强制输出为数组。它默认为false,这将解码为一个对象,这是您所需要的
$data = json_decode($jsondata); //removed boolean arg
json_decode的第二个参数采用布尔值。如果设置为true,则If强制输出为数组。它默认为false,这将解码为一个对象,这是您所需要的
$data = json_decode($jsondata); //removed boolean arg
由于您使用true作为第二个参数调用json_decode,因此您的json对象被解码为一个关联数组,而不是一个对象,因此foreach应该是
foreach($data['clanList'] as $clan
看看
协会
如果为TRUE,则返回的对象将转换为关联数组
由于您使用true作为第二个参数调用json_decode,因此您的json对象被解码为一个关联数组,而不是一个对象,因此foreach应该是
foreach($data['clanList'] as $clan
看看
协会
如果为TRUE,则返回的对象将转换为关联数组
您正在尝试作为对象检索。这是不可能的,因为第二个参数表示json输出在关联数组中。遵循下面的代码
<?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;
}
?>
您正在尝试作为对象检索。这是不可能的,因为第二个参数表示json输出在关联数组中。遵循下面的代码
<?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;
}
?>
您有一个直接错误和一个潜在错误 带有第二个参数
true
的json_decode()将返回一个关联数组(如果可以的话)。因此,foreach(和其他引用)应该使用数组索引而不是对象字段
添加true
似乎是有意的,因此我假设您希望将数据用作关联数组,而不是对象。当然,您可以删除true
参数
因为您有一个外部数据源,所以可能仍然会出现错误。例如,对于无效的json,json_decode()也可能返回false
如果您使用的是PHP5.5,那么可以使用它来检索消息。否则,你可以退回到
正确的(大部分)防错代码如下所示:
$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;
}
编辑:添加了关于根据其他建议删除
true
的注释您有一个即时错误和一个潜在错误
带有第二个参数true
的json_decode()将返回一个关联数组(如果可以的话)。因此,foreach(和其他引用)应该使用数组索引而不是对象字段
添加true
似乎是有意的,因此我假设您希望将数据用作关联数组,而不是对象。当然,您可以删除true
参数
因为您有一个外部数据源,所以可能仍然会出现错误。例如,对于无效的json,json_decode()也可能返回false
如果您使用的是PHP5.5,那么可以使用它来检索消息。否则,你可以退回到
正确的(大部分)防错代码如下所示:
$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;
}
编辑:添加了关于根据其他建议删除
true
的注释首先,您应该检查是否返回了json,然后提取它。问题是,当json被解码时,它将变成0、1、2、3等等,而您将无法获取该列表。为此,需要使用数组_值
$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");
}
首先,您应该检查是否返回了json,然后提取它。问题是,当json被解码时,它将变成0、1、2、3等等,而您将无法获取该列表。为此,需要使用数组_值
$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");
}
Protip:
var\u dump($data)代码>。这是您所期望的吗?为什么传递true
而不知道它在做什么?RTFM:2参数:如果为true,返回的对象将转换为关联数组。
。您强制PHP返回一个数组,然后尝试将该数组视为一个对象代码>。这是您所期望的吗?为什么传递true
而不知道它在做什么?RTFM:2参数:如果为true,返回的对象将转换为关联数组。
。您强制PHP返回一个数组,然后尝试将该数组视为对象。您是对的。json_解码($jsondata,true);将只返回数组而不是ObjectsPlanation:因为第二个参数设置json将转换为关联数组而不是对象。您是对的。json_解码($jsondata,true);将只返回数组而不是ObjectsPlanation:因为第二个参数设置json将转换为关联数组而不是对象。正确的方法是使用true转换为数组。因为file_get_contents无法将结果转换为对象。但是如果您使用cURL而不是file\u get\u contents,那么您可以将所有内容作为对象处理:$clan->id正确的方法是使用true转换为数组。因为file_get_contents无法将结果转换为对象。但如果您使用cURL而不是file\u get\u内容,那么您可以将所有内容作为对象处理:$clan->id