我有存储在一个纯文本文件,此JSON对象:PHP的json_decode()返回NULL与有效的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()
,它工作正常)。
我测试过JSON对http://jsonlint.com/,它是完全有效的。
这里有什么问题?
解决方案
寻找在谷歌的回答,我回到SO:json_decode returns NULL after webservice call。我的JSON文件具有UTF BOM序列(一些不应该存在的二进制字符),因此打破了JSON结构。去了十六进制编辑器,擦除了字节。一切都恢复正常。 这是为什么发生? 因为我使用Microsoft Windows'记事本编辑了文件。可怕的想法!
使用PHP 5.2。9;因此,我不能使用'json_last_error()'。 –
还要注意,这可能会发生在文件中间的其他无效字符。我只是让json_decode()返回null,因为这个字符串包含了一个特殊的连字符,可能是从MS Word粘贴的,然后可能是错误编码的。要识别潜在的问题字符,请打开JSON文件(我在Notepad ++中使用),更改编码(不转换),并保存为副本。然后比较两个文件(我使用WinMerge)。 – LinusR
(Windows的记事本问题)请咨询这个,我也分享了这个问题,它修复了它:http://stackoverflow.com/questions/10290849/how-to-remove-multiple-utf-8-bom-sequences-before -doctype –