我将此 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 在 webservice call 之后返回 NULL 。我的 JSON 文件具有 UTF BOM 序列(一些不应该存在的二进制字符),因此破坏了 JSON 结构。去十六进制编辑器,擦除字节。一切恢复正常。为什么会这样? 因为我使用 Microsoft Windows 的记事本编辑了文件。 可怕的想法!
原文由 Joel A. Villarreal Bertoldi 发布,翻译遵循 CC BY-SA 4.0 许可协议
它可能是特殊字符的编码。您可以要求 json_last_error() 获得明确的信息。