我正在创建一个插件,通过编码的URL从外部服务器获取数据。为了解码插件的输出我正在使用JsonDecode,但它每次都会返回'NULL'。
代码:
$json = file_get_contents($goodurl);
$data = json_decode($json);
var_dump($data);
这是请求网址时的响应:
{
"schemaVersion": "1.0",
"catalogueVersion": "4.0.5651.20898",
"encoding": "UTF-8",
"action": "info",
"query": "catalogue",
"success": "true",
"response": {
"title": "Presentations 2Go",
"description": "Presentations2Go videoserver is an innovative solution to simply capture presentations and\/or lectures on video. The video image of the presenter is linked with the presentation and stored for later (re)use.",
"logoUrl": "https:\/\/DEMO-FORMS.presentations2go.eu\/P2G\/images\/logo\/logo.png",
"languages": [
{"lang": "en"},
{"lang": "nl"},
{"lang": "de"},
{"lang": "fr"},
{"lang": "kr"},
{"lang": "no"},
{"lang": "pt"},
{"lang": "dk"},
{"lang": "cn"},
{"lang": "ar"},
{"lang": "se"},
{"lang": "it"},
{"lang": "es"},
{"lang": "ru"},
{"lang": "cy"},
{"lang": "tw"}
],
"videoCount": "380",
"webmaster": "support@presentations2go.eu"
}
}
这是JSON中的输出:
object(stdClass)#83 (7) {
["schemaVersion"]=> string(3) "1.0"
["catalogueVersion"]=> string(14) "4.0.5651.20898"
["encoding"]=> string(5) "UTF-8"
["action"]=> string(4) "info"
["query"]=> string(9) "catalogue"
["success"]=> string(4) "true"
["response"]=> object(stdClass)#84 (6) {
["title"]=> string(17) "Presentations 2Go"
["description"]=> string(207) "Presentations2Go videoserver is an innovative solution to simply capture presentations and/or lectures on video. The video image of the presenter is linked with the presentation and stored for later (re)use."
["logoUrl"]=> string(63) "https://DEMO-FORMS.presentations2go.eu/P2G/images/logo/logo.png"
["languages"]=> array(16) {
[0]=> object(stdClass)#85 (1) {
["lang"]=> string(2) "en"
}
[1]=> object(stdClass)#86 (1) {
["lang"]=> string(2) "nl"
}
[2]=> object(stdClass)#87 (1) {
["lang"]=> string(2) "de"
}
[3]=> object(stdClass)#88 (1) {
["lang"]=> string(2) "fr"
}
[4]=> object(stdClass)#89 (1) {
["lang"]=> string(2) "kr"
}
[5]=> object(stdClass)#90 (1) {
["lang"]=> string(2) "no"
}
[6]=> object(stdClass)#91 (1) {
["lang"]=> string(2) "pt"
}
[7]=> object(stdClass)#92 (1) {
["lang"]=> string(2) "dk"
}
[8]=> object(stdClass)#93 (1) {
["lang"]=> string(2) "cn"
}
[9]=> object(stdClass)#94 (1) {
["lang"]=> string(2) "ar"
}
[10]=> object(stdClass)#95 (1) {
["lang"]=> string(2) "se"
}
[11]=> object(stdClass)#96 (1) {
["lang"]=> string(2) "it"
}
[12]=> object(stdClass)#97 (1) {
["lang"]=> string(2) "es"
}
[13]=> object(stdClass)#98 (1) {
["lang"]=> string(2) "ru"
}
[14]=> object(stdClass)#99 (1) {
["lang"]=> string(2) "cy"
}
[15]=> object(stdClass)#100 (1) {
["lang"]=> string(2) "tw"
}
}
["videoCount"]=> string(3) "378"
["webmaster"]=> string(27) "support@presentations2go.eu"
}
}
我不是那种经验丰富的JSON所以我怀疑那里可能存在语法错误。但我不确定是什么以及如何解决它。
答案 0 :(得分:1)
您正在获取对象而不是数组。 只需写下:
$data = json_decode($json, true);
接收关联数组。
答案 1 :(得分:0)
使用此代码
$data = json_decode($json,true);
答案 2 :(得分:0)
您正在获取一个对象而不是一个数组。
接收关联数组。
$data = json_decode($json, true);
或者您可以访问或获取对象的值,如
echo $data->catalogueVersion;
答案 3 :(得分:0)
每个人都已经告诉过你将json_decode
设置为true,当真正返回的对象将被转换为关联数组时,link
$data = json_decode($json, true);
我建议使用以下代码查看您的数组。它更容易阅读。
echo "<pre>";
print_r($array);
echo "</pre>";