php讀取解析json檔案

來源:互聯網
上載者:User
 ";  $json2_string='{  "tsnpm":{    "name":"#特色牛排面#","score":"(4.7分)","price":"¥9.0","description":"較勁十足,微辣暢爽。"  },  "mnnrm":{    "name":"#:閩南牛肉麵#","score":"(4.8分)","price":"¥8.5","description":"爽滑可口,回味無窮。"  },  "hxncm":{    "name":"#紅燒牛雜麵#","score":"(4.8分)","price":"¥8.0","description":"清香誘人,五味雜成!"  },  "gwcsgjf":{    "name":"#廣味叉燒蓋澆飯#","score":"(4.7分)","price":"¥8.5","description":"滋補,養胃、美味、有勁"  },   "xhscd":{    "name":"#西紅柿炒蛋#","score":"(4.8分)","price":"¥2.0","description":"口味怡人,物美價廉!"  }}'; $obj=json_decode($json2_string,true); if (!is_array($obj)) die('no successful'); print_r($obj);     echo '
'.$obj['tsnpm']['name'];?>

當我用解析$json2_string字串時可以解析成功,但是我解析從檔案中擷取來的字串$json_string時就解析不了了,請問高手這該怎麼解決?


回複討論(解決方案)

你的json 檔案是帶有BOM頭的UTF-8格式吧?去掉BOM頭看下

哪幅是$json2_string的 , 分開貼一下。

./js/recommendMenu.json 用串連的形式開啟看看行不行?

echo print_r($json_string,true);
只是列印了讀取到的字串

沒有在你的代碼中看到 json_decode($json_string) 字樣,你怎麼就知道不能解析呢?

$obj=json_decode($json2_string,true);
if (!is_array($obj)) die('no successful');

有提示?

你的json 檔案是帶有BOM頭的UTF-8格式吧?去掉BOM頭看下


一樓說的對,我的編碼是BOM頭的utf-8格式,去掉了就和字串的一樣

./js/recommendMenu.json 用串連的形式開啟看看行不行?


有的 在第39行

就是一樓所說的問題 我用的編碼不對,謝謝大家熱心的回複

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.