在php中显示JSON数据

时间:2014-01-20 18:14:09

标签: php json

从SDK中我的json数据显示为:

$count_categories_json = "{\"api\":
{\"version\":\"1.0\",\"cal_url\":\"http:\/\/www.example.com\",\"encoding\":\"ISO-8859-1\",\"generated\":\"2014-01-20T17:23:21+01:00\",\"contents\":\"categories_event_count\"}
,\"0\":{\"id\":\"47\",\"name\":\"Expositions\",\"parent_id\":\"0\",\"event_count\":\"679\",\"rank\":1}
,\"1\":{\"id\":\"94\",\"name\":\"Gratuit\",\"parent_id\":\"56\",\"event_count\":\"598\",\"rank\":2}
…}

显示下面的代码

  

事件 -

     

679个事件 - 博览会

     

598项活动 - Gratuit

$array = json_decode($count_categories_json,true);

    foreach ($array as $jsons) {
        echo '<li>'.$jsonsc3['event_count'].' events - '.$jsonsc3['name'].'</li>';
    }

第一个lign当然是不受欢迎的,来自json的第一个元素(“api”:{\“version \”:\“1.0 \”,\ ...)

问题是:怎么不发信呢?

2 个答案:

答案 0 :(得分:1)

$array = json_decode($count_categories_json,true);

array_shift($array);

foreach...

array_shift()将删除第一个数组元素。

答案 1 :(得分:0)

foreach之前,添加以下内容:

unset($array['api']);

这将从您的JSON对象中删除'api'属性。

或者,在foreach循环中添加:

if ( !isset($jsons['id']) ) continue;

这将跳过没有定义“id”属性的每一行。