2012-11-16 54 views
1

我是json的新手。我想從JSON文件讀取數據到php文件中。這是簡單的方法來讀取JSON到PHP?

我的JSON文件中的數據是這樣的

{"multicast_id":8925377088262649527,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"NotRegistered"}]} 

我只是想檢查successfailure值。我用

json_decode($jason_string,true); 

但它不能在我的服務器上工作。我不知道原因。

所以請告訴我解決這個問題最簡單的方法,請詳細解釋。

+1

爲什麼json_decode在您的服務器上無法正常工作?你會得到什麼錯誤? – m4t1t0

+1

當你使用json_decode時你有任何錯誤嗎?你使用的是什麼版本的PHP? – LeonardChallis

+0

它給了我這個錯誤'致命錯誤:調用未定義的函數:json_decode()在C:\ inetpub \ vhosts \ hariomcards.com \ httpdocs \ QRme \ Testing.php在線14' – nishitpatel

回答

1

你的代碼應該工作。

<?php 
$somestring = <<<HEREDOC 
{"multicast_id":8925377088262649527,"success":0,"failure":1,"canonical_ids":0,"results"$ 
HEREDOC; 

var_dump(json_decode($somestring, true)); 

產量預期:

[email protected]:~$ php test.php 
array(5) { 
    ["multicast_id"]=> 
    int(8925377088262649527) 
    ["success"]=> 
    int(0) 
    ["failure"]=> 
    int(1) 
    ["canonical_ids"]=> 
    int(0) 
    ["results"]=> 
    array(1) { 
    [0]=> 
    array(1) { 
     ["error"]=> 
     string(13) "NotRegistered" 
    } 
    } 
} 

請啓用日誌記錄PHP調試目的! 看看:http://davidwinter.me/articles/2009/07/14/enable-php-error-logging/

如果它是一個本地開發服務器我寧願顯示錯誤,並讓所有的錯誤消息 http://www.dzone.com/snippets/let-php-show-all-errors

1

做到這一點:

進入 - >開始 - >運行 - > CMD.EXE

運行:

c:\path\to\your\php.exe --version 

檢查,如果你有PHP 5.2或更高版本。 (JSON它與PHP默認從5.2捆綁),如果你不..你需要安裝(http://www.php.net/manual/en/install.pecl.windows.php)或升級PHP到較新的版。

ü可能也試着做這個殼

c:\path\to\your\php.exe --ini 

編輯,顯示在

Loaded Configuration File: 

前php.ini中找到行

;延長= php_json.dll

刪除';'並保存,如果你沒有線可以添加它。重新啓動您的Web服務器並重試

相關問題