php读取解析json文件

WBOY
Release: 2016-06-23 13:58:38
Original
1128 people have browsed it

 <?php      $filename = "./js/recommendMenu.json";	 $json_string = file_get_contents($filename);	 echo print_r($json_string,true);            //打印文件的内容	 echo "<br>";	 	 $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 '<br>'.$obj['tsnpm']['name'];	?>
Copy after login

当我用解析$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行

就是一楼所说的问题  我用的编码不对,谢谢大家热心的回复

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!