2011-05-12 101 views
1

解析JSON字符串我有在JSON格式的URL返回一个复杂的结构,我有,我可以通过的var_dump查看效应初探,现在我有这个效应初探,遍历PHP

{ 
    "groups": [], 
    "total_pages": 1, 
    "spots": [ 
    { 
     "address": { 
     "region": "TX", 
     "locality": "Austin" 
     }, 
     "name": "Dirty Bill's", 
     "checkins_count": 646, 
     "_image_url_200": "http://static.gowalla.com/categories/28-b0d41920d32839ce1ecd6641e5fc2c87-200.png", 
     "image_url": "http://static.gowalla.com/categories/28-78c9b4d7d239784df49dc932f64a3519-100.png", 
     "_image_url_50": "http://static.gowalla.com/categories/28-78c9b4d7d239784df49dc932f64a3519-100.png", 
     "radius_meters": 50, 
     "trending_level": 0, 
     "users_count": 375, 
     "url": "/spots/43711", 
     "checkins_url": "/checkins?spot_id=43711", 
     "lng": "-97.7495040214", 
     "spot_categories": [ 
     { 
      "name": "Dive Bar", 
      "url": "/categories/28" 
     } 
     ], 
     "foursquare_id": null, 
     "highlights_url": "/spots/43711/highlights", 
     "items_url": "/spots/43711/items", 
     "items_count": 11, 
     "strict_radius": false, 
     "description": "AKA the Gnome Bar. Much Warmer than Key Bar.", 
     "activity_url": "/spots/43711/events", 
     "lat": "30.2696322356", 
     "photos_count": 23 
    }, 
    { 
     "address": { 
     "region": "TX", 
     "locality": "Austin" 
     }, 
     "name": "Austin Wellness Clinic", 
     "checkins_count": 1, 
     "_image_url_200": "http://static.gowalla.com/categories/118-b41c2ba96f1ffe99fc23f12f0ee3b960-200.png", 
     "image_url": "http://static.gowalla.com/categories/118-5f9e72162abf3dcbc0108cdbdba6a29f-100.png", 
     "_image_url_50": "http://static.gowalla.com/categories/118-5f9e72162abf3dcbc0108cdbdba6a29f-100.png", 
     "radius_meters": 75, 
     "trending_level": 0, 
     "users_count": 1, 
     "url": "/spots/7360448", 
     "checkins_url": "/checkins?spot_id=7360448", 
     "lng": "-97.7495133877", 
     "spot_categories": [ 
     { 
      "name": "Health & Fitness", 
      "url": "/categories/118" 
     } 
     ], 
     "foursquare_id": null, 
     "highlights_url": "/spots/7360448/highlights", 
     "items_url": "/spots/7360448/items", 
     "items_count": 0, 
     "strict_radius": false, 
     "description": null, 
     "activity_url": "/spots/7360448/events", 
     "lat": "30.2695755256", 
     "photos_count": 0 
    }, 

我已经使用json_decode($ response,true)来获得一个解析变量,现在我不确定如何循环。有任何想法吗?!

编辑1:斑点是[]数组具有索引0我想环路虽然点阵列内的每个名字VALU对

+1

你想循环什么?这不是解析的问题。 – 2011-05-12 13:17:34

+1

像普通数组那样循环它,因为它是。 – 2011-05-12 13:17:43

+0

你想做什么循环?请写下你想要的阵列 – diEcho 2011-05-12 13:17:54

回答

3
<?php 

$json = json_encode(
    array(
     'spots' => array(
      'bar' => 'baz', 
      array(
       'quz' => 'foo', 
       'bar' => 'baz' 
      ) 
     ) 
    ) 
); 

$root = json_decode($json, true); 

function read($array) { 
    foreach((array) $array as $key => $value) { 
     if(is_array($value)) { 
      read($array); 
     } 
     echo "$key = $value\n"; 
    } 
} 

foreach($root['spots'] as $spot) { 
    read($spot); 
} 

这应该给你点阵列中的所有信息。

编辑:现在用实际检查的语法,它的工作原理。

+0

我得到一个解析错误,用这段代码! – Maverick 2011-05-12 13:31:38

+0

嗨。是的,对不起,错过了一个foreach,所以它出现了。无论如何,它现在有效。 – 2011-05-12 13:45:17

0

你不会遍历,你会使用recursion来完成它。您需要编写一个可以调用自身的函数,将该结构的不同分支作为参数传递给自己。 This可能会帮助您使用PHP递归。

2
$result = json_decode($response,true); 

foreach($result['spots'] as $spot) 
{ 
    echo $spot['address']['locality']; 
} 
2

或者试试这个:

 
$result = json_decode($response,true); 
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($result)); 
foreach($iterator as $key=>$value) { 
     echo "<b>".$key."</b><br />".$value."<br />"; 
} 

这将转换成一个复杂的关联数组中的简单数组循环。
希望这会有所帮助。