我将此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"
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试用json_decode()它解码时,它返回NULL.为什么?该文件是可读的(我尝试回声file_get_contents(),它工作正常).
我已经针对http://jsonlint.com/测试了JSON ,它完全有效.
这有什么不对?
在Google上寻找答案,我回到了SO:json_decode在webservice调用后返回NULL.我的JSON文件具有UTF BOM序列(一些不应该存在的二进制字符),因此打破了JSON结构.去十六进制编辑器,删除了字节.一切都恢复正常.为什么会这样?因为我使用Microsoft Windows的记事本编辑了该文件.糟糕的主意!
Dun*_*hka 73
这对我有用
json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $json_string), true );
Run Code Online (Sandbox Code Playgroud)
Pek*_*ica 64
它可能是特殊字符的编码.您可以要求json_last_error()获取明确的信息.
更新:问题已解决,请查看问题中的"解决方案"段落.
Gab*_*ada 25
你可以尝试一下.
json_decode(stripslashes($_POST['data']))
Run Code Online (Sandbox Code Playgroud)
小智 23
如果您在chrome中检查请求,您将看到JSON是文本,因此JSON中添加了空白代码.
您可以使用清除它
$k=preg_replace('/\s+/', '',$k);
然后你可以使用:
json_decode($k)
print_r 然后会显示数组.
Yap*_*owe 13
I had the same problem and I solved it simply by replacing the quote character before decode.
$json = str_replace('"', '"', $json);
$object = json_decode($json);
Run Code Online (Sandbox Code Playgroud)
My JSON value was generated by JSON.stringify function.
小智 10
也许一些隐藏的角色正在弄乱你的json,试试这个:
$json = utf8_encode($yourString);
$data = json_decode($json);
Run Code Online (Sandbox Code Playgroud)
Nim*_*Nim 10
对我来说,php 函数 stripslashes() 在从 javascript 接收 json 时起作用。当从 python 接收 json 时,json_decode 调用的第二个可选参数就可以解决这个问题,因为数组是关联的。对我来说就像魅力一样。
$json = stripslashes($json); //add this line if json from javascript
$edit = json_decode($json, true); //adding parameter true if json from python
Run Code Online (Sandbox Code Playgroud)
此错误意味着您的 JSON 字符串不是有效的 JSON!
发生错误时启用抛出异常,PHP 将抛出异常并说明失败的原因。
用这个:
$json = json_decode($string, null, 512, JSON_THROW_ON_ERROR);
Run Code Online (Sandbox Code Playgroud)
小智 6
$k=preg_replace('/\s+/', '',$k);
Run Code Online (Sandbox Code Playgroud)
为我做了 是的,在Chrome上测试.请转至user2254008
我只是想添加这个,因为我今天遇到了这个问题。如果 JSON 字符串周围有任何字符串填充,则 json_decode 将返回 NULL。
如果您从 PHP 变量以外的源提取 JSON,那么明智的做法是先“修剪”它:
$jsonData = trim($jsonData);
Run Code Online (Sandbox Code Playgroud)
小智 6
这可以帮助您了解错误的类型
<?php
// A valid json string
$json[] = '{"Organization": "PHP Documentation Team"}';
// An invalid json string which will cause an syntax
// error, in this case we used ' instead of " for quotation
$json[] = "{'Organization': 'PHP Documentation Team'}";
foreach ($json as $string) {
echo 'Decoding: ' . $string;
json_decode($string);
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
echo PHP_EOL;
}
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
194642 次 |
| 最近记录: |