在php中循环解析json字符串

发布于 2024-11-06 09:01:03 字数 2519 浏览 0 评论 0原文

我有一个由 JSON 格式的 URL 返回的复杂结构,我得到了可以通过 var_dump 查看的响应,现在我有了这个响应,

{
  "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。我想循环遍历点数组内的每个名称值对

I have a complicated structure of returned by an URL in JSON format, i have got the reponse which I can view through var_dump, Now I have this reponse,

{
  "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
    },

I have used json_decode($response,true) to get a parse variable, now I am not sure as to how to loop through it. Any ideas?!

edit 1: The spots is an array[] which has an index 0. I want to loop though each name valu pair inside the spots array

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浮生未歇 2024-11-13 09:01:03
<?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 );
}

这应该为您提供点数组内的所有信息。

编辑:现在通过实际检查的语法,它可以工作了。

<?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 );
}

This should give you all the information inside the spot array.

EDIT: Now with actually checked syntax, it works.

鲸落 2024-11-13 09:01:03
$result = json_decode($response,true);

foreach($result['spots'] as $spot)
{
    echo $spot['address']['locality'];
}
$result = json_decode($response,true);

foreach($result['spots'] as $spot)
{
    echo $spot['address']['locality'];
}
烂人 2024-11-13 09:01:03

或者尝试这个:

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

这会将复杂的关联数组转换为简单的循环数组。

希望这有帮助。

Or try this one:

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

This will convert a complex associative array in a simple array for looping.

Hope this helps.

梦屿孤独相伴 2024-11-13 09:01:03

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

You wouldn't loop through that, you'd use recursion to get through it. You need to write a function that can call itself, passing different branches of that structure to itself as the arguments. This might help you with PHP recursion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文