Home  >  Article  >  Backend Development  >  php处理json数据的问题

php处理json数据的问题

WBOY
WBOYOriginal
2016-06-23 13:44:24801browse

我连接一个开放平台,返回一个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 '<meta http-equiv="content-type" content="text/html; charset=utf-8">';$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'].'<br>';    echo 'CardID:'.$val['CardID'].'<br>';    echo 'Title:'.$val['Title'].'<br><br>';}


Gid:123
CardID:15301
Title:标题一

Gid:456
CardID:15333
Title:标题二

Gid:789
CardID:15405
Title:标题三

非常感谢,是对的,谢谢。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn