[英]Getting null in json_decode
我有一個帶有json的文本文件。 下面的例子:
{'something' : 'ss'}
我正在嘗試在php上讀取它,並使用json_decode
將其轉換為數組。
$temp = '';
$fh = fopen( '/quiz' . $testid . '.txt' ,'r');
while ($line = fgets($fh)) {
$temp .= $line;
}
fclose($fh);
$temp = str_replace("\n","",$temp); //to remove new line
$temp = str_replace("\r","",$temp);
$temp = json_decode($temp);
但我越來越null
如果我不json_decode
則可以獲取字符串。
我希望任何人都可以幫助我。
謝謝,E
您無需在調用json_decode
之前進行任何解析
$contents = file_get_contents('/quiz' . $testid . '.txt');
$temp = json_decode($contents);
如果仍然為空,則您的JSON可能無效,您可以使用json_last_error進行診斷。
該代碼段有效,因此我認為您的文件解析算法存在問題
<?php
$temp = '';
$temp='{
"a":1,
"b":[1,2,3]
}';
$temp = str_replace("\n","",$temp); //to remove new line
$temp = str_replace("\r","",$temp);
$temp = json_decode($temp);
var_dump($temp);
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.