php正则匹配截取json

苏生不惑 发布于 2013/08/27 17:06
阅读 1K+
收藏 0
PHP

 请教一个正则截取问题,@Tuesday

$script='<script type="text/javascript">
cgiData={
pageIdx : 0,
pageCount : 1,
pageSize : 100,
groupsList : ({"groups":[{"id":0,"name":"未分组","cnt":16},{"id":1,"name":"黑名单","cnt":0},{"id":2,"name":"星标组","cnt":0}]}).groups,
        friendsList : ({"contacts":[{"id":6,"nick_name":"yan","remark_name":"","group_id":0},{"id":8,"nick_name":"mm","remark_name":"","group_id":0},{"id":4,"nick_name":"gg","remark_name":"","group_id":0},{"id":2,"nick_name":"sa","remark_name":"","group_id":0}]}).contacts,
            currentGroupId : 0,
        type : 0,
    userRole : 1 * 1,
    verifyMsgCount :  * 1
};
seajs.use("user/index");
</script>';
怎么把[{"id":6,"nick_name":"yan","remark_name":"","group_id":0},{"id":8,"nick_name":"mm","remark_name":"","group_id":0},{"id":4,"nick_name":"gg","remark_name":"","group_id":0},{"id":2,"nick_name":"sa","remark_name":"","group_id":0}]这段截取出来?
加载中
0
苏生不惑
苏生不惑
preg_match('%contacts\"\:([\s\S]*?)(?=}\))%', $json, $match);解决啦
0
Legend_x
Legend_x
为什么不通过属性取出来?
0
mingkaidox
mingkaidox
这个JSON.parse/stringify来做也可以。
//run this in console
JSON.stringify(cgiData.friendsList)
//then we get this string
"[{"id":6,"nick_name":"yan","remark_name":"","group_id":0},{"id":8,"nick_name":"mm","remark_name":"","group_id":0},{"id":4,"nick_name":"gg","remark_name":"","group_id":0},{"id":2,"nick_name":"sa","remark_name":"","group_id":0}]"

mingkaidox
mingkaidox
^_^
苏生不惑
苏生不惑
嗯,谢谢json不错,但我是在服务器端进行截取
0
Jackin
Jackin
if(preg_match('#\(\{"contacts":(?P<friendsList>.+?)\}\)\.contacts#S', $html, $match)){
		echo $match['friendsList'];
	}
Jackin
Jackin
回复 @苏生不惑 :不知道的你可以print_r 打印出来看看呗!
苏生不惑
苏生不惑
谢谢,(?P<friendsList>.+?)这是什么意思
返回顶部
顶部