简体   繁体   English

无法访问由 json_decode 创建的 Json 数组值

[英]Cant access Json Array values created by json_decode

My json_decode() is returning an array but i just cant seem to access it.我的 json_decode() 正在返回一个数组,但我似乎无法访问它。 Here's as much of information i think would be useful.这是我认为有用的信息。 I'm pulling a json value from a server using curl commands.我正在使用 curl 命令从服务器中提取 json 值。

The response (output) from the server:来自服务器的响应(输出):

//php code from getVal.php 
$response = curl_exec($ch); 
echo($response);
var_dump($response);

在此处输入图像描述

{ "automat": false, "brightness": null, "increase: set time": 2, "decrease: set time": 2 } string(101) "{ "automat": false, "brightness": null, "increase: set time": 2, "decrease: set time": 2 } "     
 

Converting the json formatted string to json array:将 json 格式化字符串转换为 json 数组:

//php code from getVal.php 
$res = json_decode($response, true);
echo($res);
echo("<br><br>");
var_dump($res);
echo("<br><br>");
echo($res["automat"]);

output: output: 在此处输入图像描述

Array

array(4) { ["automat"]=> bool(false) ["brightness"]=> NULL ["increase: set time"]=> int(2) ["decrease: set time"]=> int(2) }

Clearly $res prints as Array for some reason, and when trying to print $res["automat"] it doesn't t print anything at all.显然 $res 出于某种原因打印为 Array ,并且在尝试打印 $res["automat"] 时它根本不打印任何内容。 I've checked the console and there is no errors.我检查了控制台,没有错误。

Solutions already tried with no result:已经尝试过的解决方案没有结果:

//1: 
$response =  stripslashes($response); 
//2: 
$response = html_entity_decode($response);
//3: 
$nbsp = html_entity_decode( "&nbsp;" );
$response = str_replace( $nbsp, "", $response);
//for each above 
$res = json_decode($response, true); 

Any further information can be provided if asked.如果询问,可以提供任何进一步的信息。

You are trying to echo a bool with a false value.您正在尝试使用false值回显布尔值。 It will not return/print anything.它不会返回/打印任何东西。

You can test this, be executing the following code:您可以对此进行测试,执行以下代码:

echo '('. NULL .')' . "\r";
echo '('. false .')'. "\r";
echo '('. true .')' . "\r";

This will give your output:这将为您提供 output:

()
()
(1)

In order to see something from your example, use another key for this:为了从您的示例中查看某些内容,请为此使用另一个键:

echo $res["increase: set time"];

PS Don't use images for showing source code. PS 不要使用图像来显示源代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM