json_decode无法解析\t的问题
2013-04-11 14:18
169 查看
1)源代码
结果
null
2)源代码
结果
array
0 => string 'real_server 123.125.160.121 80 {' (length=32)
1 => string ' real_server 123.125.160.122 80 {' (length=33)
问题原因:json_decode 函数无法解析tab键
###################################################
以下为网上找到的一篇文章
json_decode在php中的一些无法解析的字符串
关于json_decode在php中的一些无法解析的字符串,包括以下几种常见类型。
一、Bug #42186 json_decode() won't work with \l
当字符串中含有\l的时候,json_decode是无法解析,测试代码:
echo "***********json_decode() won't work with \l*************<br/>";
$json = '{"stringwithbreak":"line with a \lbreak!"}';
var_dump($json);//stringwithbreak":"line with a \lbreak!
var_dump(json_decode($json, true));//null
解决办法:
主要是将\l进行替换,当然如果真的需要‘\l’,我们就必须不使用json_decode进行解析,可以当作当个字符进行提交。
var_dump(str_replace("\\l", "", $json));//stringwithbreak":"line with a break!
print_r(json_decode(str_replace("\\l", "", $json), true));//Array ( [stringwithbreak] => line with a break! )
二、Tabs in Javascript strings break json_decode()
当字符串中含有tab键时,json_decode()无法解析,例如代码3-1
echo "<br/>***********Tabs in Javascript strings break json_decode()*************<br/>";
var_dump(json_decode('{ "abc": 12, "foo": "bar
bar" }'));
执行后的返回结果为null
解决办法:
1、当遇到含有tab键输入的字符串时,我们应该避免使用json将数据传到php,然后使用php作为解析。
2、同样可以使用如下3-2代码方式进行替换
$myStr = '{ "abc": 12, "foo": "bar bar" }';
$replaceStr = str_replace(" ", "\\t", $myStr);
var_dump($replaceStr);
var_dump(json_decode($replaceStr ));
三、json_decode returns false when leading zeros aren't escaped with double quotes
当json的value值为number类型,而且该number以0开头,例如代码4-1
echo "<br/>***********json_decode returns false when leading zeros aren't escaped with double quotes*************<br/>";
$noZeroNumber = '{
"test" : 6
}';
$zeroNumber= '{
"test" : 06
}';
var_dump(json_decode($noZeroNumber));//object(stdClass)[1]
public 'test' => int 6
var_dump(json_decode($zeroNumber));//null
或许对于这种问题很少出现,但是一旦出现了,我们就很难去查找问题的原因。
四、decode chokes on unquoted object keys
当key值没有使用引号时,会无法解析,例如代码5-1
echo "<br/>***********decode chokes on unquoted object keys*************<br/>";
var_dump(json_decode('{"a":"tan","model":"sedan"}'));//object(stdClass)[1]
public 'a' => string 'tan' (length=3)
public 'model' => string 'sedan' (length=5)
var_dump(json_decode('{a:"tan","model":"sedan"}'));//null
<?php $ret="[\"real_server 123.125.160.121 80 {\",\" real_server 123.125.160.122 80 {\"]"; $jd = json_decode($ret); var_dump($jd); ?>
结果
null
2)源代码
<?php $ret="[\"real_server 123.125.160.121 80 {\",\" real_server 123.125.160.122 80 {\"]"; $ret=preg_replace("/\t/", " ", $ret); $jd = json_decode($ret); var_dump($jd); ?>
结果
array
0 => string 'real_server 123.125.160.121 80 {' (length=32)
1 => string ' real_server 123.125.160.122 80 {' (length=33)
问题原因:json_decode 函数无法解析tab键
###################################################
以下为网上找到的一篇文章
json_decode在php中的一些无法解析的字符串
关于json_decode在php中的一些无法解析的字符串,包括以下几种常见类型。
一、Bug #42186 json_decode() won't work with \l
当字符串中含有\l的时候,json_decode是无法解析,测试代码:
echo "***********json_decode() won't work with \l*************<br/>";
$json = '{"stringwithbreak":"line with a \lbreak!"}';
var_dump($json);//stringwithbreak":"line with a \lbreak!
var_dump(json_decode($json, true));//null
解决办法:
主要是将\l进行替换,当然如果真的需要‘\l’,我们就必须不使用json_decode进行解析,可以当作当个字符进行提交。
var_dump(str_replace("\\l", "", $json));//stringwithbreak":"line with a break!
print_r(json_decode(str_replace("\\l", "", $json), true));//Array ( [stringwithbreak] => line with a break! )
二、Tabs in Javascript strings break json_decode()
当字符串中含有tab键时,json_decode()无法解析,例如代码3-1
echo "<br/>***********Tabs in Javascript strings break json_decode()*************<br/>";
var_dump(json_decode('{ "abc": 12, "foo": "bar
bar" }'));
执行后的返回结果为null
解决办法:
1、当遇到含有tab键输入的字符串时,我们应该避免使用json将数据传到php,然后使用php作为解析。
2、同样可以使用如下3-2代码方式进行替换
$myStr = '{ "abc": 12, "foo": "bar bar" }';
$replaceStr = str_replace(" ", "\\t", $myStr);
var_dump($replaceStr);
var_dump(json_decode($replaceStr ));
三、json_decode returns false when leading zeros aren't escaped with double quotes
当json的value值为number类型,而且该number以0开头,例如代码4-1
echo "<br/>***********json_decode returns false when leading zeros aren't escaped with double quotes*************<br/>";
$noZeroNumber = '{
"test" : 6
}';
$zeroNumber= '{
"test" : 06
}';
var_dump(json_decode($noZeroNumber));//object(stdClass)[1]
public 'test' => int 6
var_dump(json_decode($zeroNumber));//null
或许对于这种问题很少出现,但是一旦出现了,我们就很难去查找问题的原因。
四、decode chokes on unquoted object keys
当key值没有使用引号时,会无法解析,例如代码5-1
echo "<br/>***********decode chokes on unquoted object keys*************<br/>";
var_dump(json_decode('{"a":"tan","model":"sedan"}'));//object(stdClass)[1]
public 'a' => string 'tan' (length=3)
public 'model' => string 'sedan' (length=5)
var_dump(json_decode('{a:"tan","model":"sedan"}'));//null
相关文章推荐
- Android开发 json解析包含图片地址无法显示图片的问题
- 解决fastjson、Jackson、Gson解析Json数据时,key为Java中关键字无法解析的问题
- php json_decode无法解析特殊问好字符
- json编码无法解析问题总结
- php中json_decode无法解析原因
- php json_decode解析json文件问题。
- 关于ajaxFileUpload无法解析json的问题(Resource interpreted as Document but transferred with ……)
- 微信小程序获取不到unionid还有小程序无法解析JSON字符串的问题
- 解决json object转string,value值存在特殊符号,无法解析问题
- IE6中运行EXTJS中某些组件无法解析DOMINO产生的JSON数据问题的解决办法
- PHP中遇到BOM、<feff>编码导致json_decode函数无法解析问题
- 【soapUI】解决post请求时,body为json串,服务器无法解析问题
- Extjs 使用fileText 文件上次后 无法解析后台返回的json问题
- PHP json_decode函数无法解析json2.js生成json的问题
- 关于json解析的无法接收报空指针的问题
- PHP接收IOS post过来的json数据无法解析的问题
- PHP json_decode 无法解析特殊问号字符
- 解决mysql5.7及以下版本无法解析json字符串的问题
- 解决json string转object,value值存在英语双引号,无法解析问题
- php下POST json数据无法解析问题