PHP json_decode() 使用有效的 JSON 返回 NULL? [英] PHP json_decode() returns NULL with valid JSON?

查看:37
本文介绍了PHP json_decode() 使用有效的 JSON 返回 NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将此 JSON 对象存储在纯文本文件中:

I have this JSON object stored on a plain text file:

{
    "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() 并且它工作正常).

When I try to decode it with json_decode(), it returns NULL. Why? The file is readable (I tried echoing file_get_contents() and it worked ok).

我已经针对 http://jsonlint.com/ 测试了 JSON,它完全有效.

I've tested JSON against http://jsonlint.com/ and it's perfectly valid.

这里出了什么问题?

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

Looking for answers on Google, I got back to SO: json_decode returns NULL after webservice call. My JSON file had the UTF BOM sequence (some binary chars that shouldn't be there), thus, breaking the JSON structure. Went to Hex Editor, erased the bytes. Everything's back to normal. Why has this happened? Because I edited the file using Microsoft Windows' Notepad. Terrible idea!

推荐答案

可能是特殊字符的编码.您可以询问 json_last_error() 以获得明确信息.

It could be the encoding of the special characters. You could ask json_last_error() to get definite information.

更新:问题已解决,请查看问题中的解决方案"段落.

Update: The issue is solved, look at the "Solution" paragraph in the question.

这篇关于PHP json_decode() 使用有效的 JSON 返回 NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆