我将这个JSON对象存储在一个纯文本文件中:
{
"MySQL": {
"Server": "(server)",
"Username": "(user)",
"Password": "(pwd)",
"DatabaseName": "(dbname)"
},
"Ftp": {
"Server": "(server)",
"Username": "(user)",
"Password": "(pwd)",
"RootFolder": "(rf)"
},
"BasePath": "../../bin/",
"NotesAppPath": "notas",
"SearchAppPath": "buscar",
"BaseUrl": "http:\/\/montemaiztusitio.com.ar",
"InitialExtensions": [
"nem.mysqlhandler",
"nem.string",
"nem.colour",
"nem.filesystem",
"nem.rss",
"nem.date",
"nem.template",
"nem.media",
"nem.measuring",
"nem.weather",
"nem.currency"
],
"MediaPath": "media",
"MediaGalleriesTable": "journal_media_galleries",
"MediaTable": "journal_media",
"Journal": {
"AllowedAdFileFormats": [
"flv:1",
"jpg:2",
"gif:3",
"png:4",
"swf:5"
],
"AdColumnId": "3",
"RSSLinkFormat": "%DOMAIN%\/notas\/%YEAR%-%MONTH%-%DAY%\/%TITLE%/",
"FrontendLayout": "Flat",
"AdPath": "ad",
"SiteTitle": "Monte Maíz: Tu Sitio",
"GlobalSiteDescription": "Periódico local de Monte Maíz.",
"MoreInfoAt": "Más información aquí, en el Periódico local de Monte Maíz.",
"TemplatePath": "templates",
"WeatherSource": "accuweather:SAM|AR|AR005|MONTE MAIZ",
"WeatherMeasureType": "1",
"CurrencySource": "cotizacion-monedas:Dolar|Euro|Real",
"TimesSingular": "vez",
"TimesPlural": "veces"
}
}
当我尝试用json_decode()
解码它时,它返回NULL。为什么?该文件是可读的(我尝试回显file_get_contents()
,它工作正常)。
我已经在http://jsonlint.com/上测试过JSON,它是完全有效的。
这是怎么回事?
更新(对我2010年的问题有效的回复)
在谷歌上寻找答案,我回到了SO:json_decode returns NULL after webservice call。我的JSON文件包含UTF BOM序列(一些不应该出现的二进制字符),因此破坏了JSON结构。转到十六进制编辑器,删除了字节。一切都恢复正常了。为什么会发生这种情况?因为我使用Microsoft Windows的记事本编辑了该文件。糟糕的主意!
发布于 2014-08-18 23:55:35
这对我很有效
json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $json_string), true );
发布于 2015-11-13 12:20:33
你可以试一试。
json_decode(stripslashes($_POST['data']))
发布于 2013-04-07 15:51:18
如果你在chrome中检查请求,你会看到JSON是文本,所以JSON中添加了空白代码。
您可以使用以下命令清除它
$k=preg_replace('/\s+/', '',$k);
然后,您可以使用:
json_decode($k)
然后,print_r
将显示该数组。
https://stackoverflow.com/questions/2410342
复制相似问题