Home > Backend Development > PHP Tutorial > php处理json数据的有关问题

php处理json数据的有关问题

WBOY
Release: 2016-06-13 12:10:31
Original
804 people have browsed it

php处理json数据的问题
我连接一个开放平台,返回一个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有问题,少了两个{

<br />{"status":0,"total":4,"data":"[{\"Gid\":\"123\",\"CardID\":\"15301\",\"Title\":\"标题一\"},\"Gid\":\"456\",\"CardID\":\"15333\",\"Title\":\"标题二\"},\"Gid\":\"789\",\"CardID\":\"15405\",\"Title\":\"标题三\"}]"}<br />
Copy after login


修改为:
{"status":0,"total":4,"data":"[{\"Gid\":\"123\",\"CardID\":\"15301\",\"Title\":\"标题一\"},{\"Gid\":\"456\",\"CardID\":\"15333\",\"Title\":\"标题二\"},{\"Gid\":\"789\",\"CardID\":\"15405\",\"Title\":\"标题三\"}]"}

循环输出:
<br />echo '<meta http-equiv="content-type" content="text/html; charset=utf-8">';<br />$response = '{"status":0,"total":4,"data":"[{\"Gid\":\"123\",\"CardID\":\"15301\",\"Title\":\"标题一\"},{\"Gid\":\"456\",\"CardID\":\"15333\",\"Title\":\"标题二\"},{\"Gid\":\"789\",\"CardID\":\"15405\",\"Title\":\"标题三\"}]"}';<br />$ret = json_decode($response, true);<br />$data = json_decode($ret['data'], true);<br /><br />foreach($data as $val){<br />    echo 'Gid:'.$val['Gid'].'<br>';<br />    echo 'CardID:'.$val['CardID'].'<br>';<br />    echo 'Title:'.$val['Title'].'<br><br>';<br />}<br />
Copy after login


Gid:123
CardID:15301
Title:标题一

Gid:456
CardID:15333
Title:标题二

Gid:789
CardID:15405
Title:标题三

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template