json 文本分割 reg 表达式或解析器
$var ="
{
key : {
key_deep : val\{ue /* should be "val{ue" as { is escaped */
} ,
key2 : value
}
";
print_r(preg_split('//',$var));
// array(
// array(
// 'key'=> array(
// 'key_deep'=> 'val{ue'
// )
// ),
// array('key2'=>'value')
// );
是否有正则表达式可以使用 php 中的 preg_split 来分割它?
基本上我需要与 json_decode() 相同,但不需要 BOTH value
和 key
上的引号,唯一转义的是四个字符 \{ \, \} \:
$var ="
{
key : {
key_deep : val\{ue /* should be "val{ue" as { is escaped */
} ,
key2 : value
}
";
print_r(preg_split('//',$var));
// array(
// array(
// 'key'=> array(
// 'key_deep'=> 'val{ue'
// )
// ),
// array('key2'=>'value')
// );
is there a regular expression to split this using preg_split in php?
basically I need the same as json_decode() but without the need of the the quotes on BOTH value
and key
and the only thing escaped are four characters \{ \, \} \:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一方面,json 不正确,并且会在
json_decode
上抛出错误。此处阅读 json 的规范
json 的一种正确实现是:
此外
json_decode
永远不会产生一个Array
,它会生成一个object(stdClass)
,除非您添加true
参数Well for one thing that json is incorrect and will spew out an error on
json_decode
.read the specs for json here
One correct implementation of the json is:
Also
json_decode
never yields anArray
it yields aobject(stdClass)
unless you add thetrue
parameter考虑到此处可能发生任意嵌套,您可能想要查看解析器而不是正则表达式。
尝试:
http://pear.php.net/package/PHP_ParserGenerator/redirected
或
< a href="http://www.hwaci.com/sw/lemon/" rel="nofollow">http://www.hwaci.com/sw/lemon/
或
http://www.google.com/search ?sourceid=chrome&ie=UTF-8&q=php+解析器+生成器
You're probably going to want to look at a parser rather than a regular expression, given the arbitrary nesting that could occur here.
Try:
http://pear.php.net/package/PHP_ParserGenerator/redirected
or
http://www.hwaci.com/sw/lemon/
or
http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=php+parser+generator