簡體   English   中英

哪個是將 json 讀入 php 的簡單方法?

[英]which is the Easy and Simple way to Read json into php?

我是 json 的新手。 我想將數據從 JSON 文件讀入 php 文件。

我的json文件數據是這樣的

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

我只想檢查successfailure值。 我用了

json_decode($jason_string,true);

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

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

你的代碼應該可以工作。

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

var_dump(json_decode($somestring, true));

按預期輸出:

chris@chris-UX31E:~$ 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

做這個 :

去 -> 開始 -> 運行 -> cmd.exe

跑:

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

檢查您是否有 php 5.2 或更高版本。 (json 默認情況下從 5.2 開始與 php 捆綁在一起)如果你不......你需要安裝(http://www.php.net/manual/en/install.pecl.windows.php)或將 php 升級到更新的版本。

您也可以嘗試在 shell 中執行此操作

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

編輯前面顯示的php.ini

Loaded Configuration File:

找到線

;擴展=php_json.dll

去除那個 ';' 並保存,如果您沒有該行,則可以添加它。 重新啟動您的網絡服務器並重試

暫無
暫無

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

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