小编典典

PHP json_decode()使用有效的JSON返回NULL?

json

我将这个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 ,它非常有效。

怎么了

在Google上寻找答案,我回到SO:调用webservice后json_decode返回NULL。我的JSON文件具有UTFBOM序列(一些不应包含的二进制字符),因此破坏了JSON结构。进入十六进制编辑器,删除了字节。一切恢复正常。为什么会这样呢?
因为我使用Microsoft Windows的记事本编辑了文件。 好主意!


阅读 283

收藏
2020-07-27

共1个答案

小编典典

可能是特殊字符的编码。您可以要求 json_last_error()
获得确切的信息。

更新:问题已解决,请查看问题中的“解决方案”段落。

2020-07-27