2012-07-10 47 views
0

我在php中使用fopen來獲得JSON響應。那麼我如何去使用json decode()返回什麼?這是我用來獲取JSON的代碼,但是URL被阻止了。這給了我一個JSON。從fopen解析JSON數組php

<?php 

//$cmd = "ADSFQDS"; 

$file_handle = fopen("xxurlxx", "r"); 

while (!feof($file_handle)) { 

$line_of_text = fgets($file_handle); 
$parts = explode('=', $line_of_text); 

print $parts[0] . $parts[1]. "<BR>"; 
} 

fclose($file_handle); 
?> 
+3

分裂'='是信仰的一個大的飛躍。 '「myData」:「因爲一個=兩個」'可能是JSON中的有效字符串。如果您有一天會遇到一些嚴重的解析問題,請不要感到驚訝。 – TheZ 2012-07-10 15:58:36

回答

2

只是將您的讀取數據傳遞給json_decode。 我認爲$line_of_text是有效的JSON(所以沒有多行分裂):通過

<?php 

//$cmd = "ADSFQDS"; 

$file_handle = fopen("xxurlxx", "r"); 

while (!feof($file_handle)) { 

$line_of_text = fgets($file_handle); 
$json = json_decode($line_of_text, true); 

print $json['key']. "<BR>"; 
} 

fclose($file_handle); 
?>