簡體   English   中英

無法訪問由 json_decode 創建的 Json 數組值

[英]Cant access Json Array values created by json_decode

我的 json_decode() 正在返回一個數組,但我似乎無法訪問它。 這是我認為有用的信息。 我正在使用 curl 命令從服務器中提取 json 值。

來自服務器的響應(輸出):

//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 } "     
 

將 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: 在此處輸入圖像描述

Array

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

顯然 $res 出於某種原因打印為 Array ,並且在嘗試打印 $res["automat"] 時它根本不打印任何內容。 我檢查了控制台,沒有錯誤。

已經嘗試過的解決方案沒有結果:

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

如果詢問,可以提供任何進一步的信息。

您正在嘗試使用false值回顯布爾值。 它不會返回/打印任何東西。

您可以對此進行測試,執行以下代碼:

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

這將為您提供 output:

()
()
(1)

為了從您的示例中查看某些內容,請為此使用另一個鍵:

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

PS 不要使用圖像來顯示源代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM