我将这个json编码数据发送给我。 你能告诉我如何获得每个元素吗? 类似的东西:
$ticket
$customer
$user
{"ticket":{"id":"10909446","number":"152"},"customer":{"id":"3909381","fname":"","lname":"","email":"me@site.com","emails":["me@site.com"]},"user":{"fname":"Test","lname":"Me","id":17396,"role":"admin"}}
这是我的代码如何运行的基本视图。
$ret = array('html' => '');
$data = json_decode($data , true);
$ret['html'] = '<ul><li>'.$data->ticket->number.'</li></ul>';
echo json_encode($ret);
exit;
它只会打印li标签中的圆圈。
答案 0 :(得分:1)
json_decode就是您的答案。
答案 1 :(得分:1)
澄清@ Cthulhu的回答:
$test = '{"ticket":{"id":"10909446","number":"152"},"customer":{"id":"3909381","fname":"","lname":"","email":"me@site.com","emails":["me@site.com"]},"user":{"fname":"Test","lname":"Me","id":17396,"role":"admin"}}';
$data = json_decode($test);
echo $data->ticket->id;
输出
10909446
json_decode
将JSON变为stdClass
对象,然后您可以访问这些值。
$data = json_decode($test);
$ret = array();
$ret['html']='<ul><li>'.$data->ticket->number.'</li></ul>';
返回
json_encode($ret);
将返回
{"html":"<ul><li>152<\/li><\/ul>"}