我连接一个开放平台,返回一个json数据如下
$json_data = postData($url, $postData);
$array = json_decode($json_data,true);
echo ("
");
print_r(postData($url, $postData));
上面的代码就是平台给出的代码,print_r 输出的的就是下面的样子。
{"status":0,"total":4,"data":"[{\"Gid\":\"123\",\"CardID\":\"15301\",\"Title\":\"标题一\"},\"Gid\":\"456\",\"CardID\":\"15333\",\"Title\":\"标题二\"},\"Gid\":\"789\",\"CardID\":\"15405\",\"Title\":\"标题三\"}]"}
我想循环输出data里的Title,CarID等数据,不知道应该怎么转换和调用
我能通过
echo ("
");
echo "total=".$array['total'];
读取到total的值,但是data中的相关字段,不知道怎么读取和输出。麻烦帮着看下。
回复讨论(解决方案)
你返回的json有问题,少了两个{
{"status":0,"total":4,"data":"[{\"Gid\":\"123\",\"CardID\":\"15301\",\"Title\":\"标题一\"},\"Gid\":\"456\",\"CardID\":\"15333\",\"Title\":\"标题二\"},\"Gid\":\"789\",\"CardID\":\"15405\",\"Title\":\"标题三\"}]"}
修改为:
{"status":0,"total":4,"data":"[{\"Gid\":\"123\",\"CardID\":\"15301\",\"Title\":\"标题一\"}, {\"Gid\":\"456\",\"CardID\":\"15333\",\"Title\":\"标题二\"}, {\"Gid\":\"789\",\"CardID\":\"15405\",\"Title\":\"标题三\"}]"}
循环输出:
echo '';$response = '{"status":0,"total":4,"data":"[{\"Gid\":\"123\",\"CardID\":\"15301\",\"Title\":\"标题一\"},{\"Gid\":\"456\",\"CardID\":\"15333\",\"Title\":\"标题二\"},{\"Gid\":\"789\",\"CardID\":\"15405\",\"Title\":\"标题三\"}]"}';$ret = json_decode($response, true);$data = json_decode($ret['data'], true);foreach($data as $val){ echo 'Gid:'.$val['Gid'].'
'; echo 'CardID:'.$val['CardID'].'
'; echo 'Title:'.$val['Title'].'
';}
Gid:123
CardID:15301
Title:标题一
Gid:456
CardID:15333
Title:标题二
Gid:789
CardID:15405
Title:标题三
非常感谢,是对的,谢谢。