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

查看:119
本文介绍了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在网络服务调用后返回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天全站免登陆