首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从php接收json数据?

如何从php接收json数据?
EN

Stack Overflow用户
提问于 2013-01-05 12:46:37
回答 4查看 2.5K关注 0票数 0

我正在尝试使用API。它返回json。以下是输出:

代码语言:javascript
复制
{
    "meta": {
        "code": 200,
        "next": "http://api.yipit.com/v1/deals/?limit=1&key=AKVP839qHEM7TgXV&offset=1",
        "previous": null
    },
    "response": {
        "deals": [
            {
                "active": 1,
                "business": {
                    "id": 11009,
                    "locations": [
                        {
                            "address": "120 Millenium Dr",
                            "id": 974228,
                            "lat": 44.5400434,
                            "locality": "Chester",
                            "lon": -64.2325975,
                            "phone": null,
                            "smart_locality": "Chester",
                            "state": "NS",
                            "zip_code": "B0J 1J0"
                        },
                        {
                            "address": "95 Wentworth St",
                            "id": 974229,
                            "lat": 44.6685886,
                            "locality": "Dartmouth",
                            "lon": -63.5698711,
                            "phone": null,
                            "smart_locality": "Dartmouth",
                            "state": "NS",
                            "zip_code": "B2Y 2T3"
                        }
                    ],
                    "name": "Fit Body Boot Camp",
                    "url": "http://www.fitbodybootcampbeachwood.com/"
                },
                "date_added": "2013-01-05 04:14:17",
                "description": "Exercise keeps the body in peak condition, which explains the marbleized ThighMaster lying next to Michelangelo's David. Enjoy an anatomical renaissance with this Groupon. Choose Between Two Options  $29 for a 10-day Fit and Firm program (a $124 value) $47 for a four-week Rapid Fat Loss program (a $247 value)  Both options include a nutrition consultation, elective weigh-ins and body-fat measurements, and unlimited boot-camp sessions for the duration of the program. The sessions combine cardio training with resistance and weight work, helping to maximize caloric burn both during and after the workout. Click here to see the class schedule, and click here for a list of frequently asked questions.",
                "discount": {
                    "formatted": "77%",
                    "raw": 77
                },
                "division": {
                    "active": 1,
                    "country": "Canada",
                    "lat": 44.648881,
                    "lon": -63.575312,
                    "name": "Halifax, Nova Scotia",
                    "slug": "halifax-nova-scotia",
                    "time_zone_diff": -4,
                    "url": "http://yipit.com/halifax-nova-scotia/"
                },
                "end_date": "2013-01-11 03:59:59",
                "id": 14517543,
                "images": {
                    "image_big": "http://b.yipitcdn.com/cache/deal/10-day-fit-and-firm-program-or-four-week-rapid-fat-loss-program-at-fit-body-boot-camp-up-to-81-off-42-1357359256_display_image.jpg",
                    "image_small": "http://a.yipitcdn.com/cache/deal/10-day-fit-and-firm-program-or-four-week-rapid-fat-loss-program-at-fit-body-boot-camp-up-to-81-off-42-1357359256_small_image.jpg"
                },
                "mobile_url": "http://m.yipit.com/halifax-nova-scotia/groupon/10-day-fit-and-firm-program-or-four-week-rapid-fat-loss-program-at-fit-body-boot-camp-up-to-81-off-42/?bp_ad=1",
                "price": {
                    "formatted": "$29",
                    "raw": 29.00
                },
                "source": {
                    "name": "Groupon",
                    "paid": 0,
                    "slug": "groupon",
                    "url": ""
                },
                "tags": [
                    {
                        "name": "Boot Camp",
                        "slug": "boot-camp",
                        "url": ""
                    }
                ],
                "title": "10-Day Fit-and-Firm Program or Four-Week Rapid Fat-Loss Program at Fit Body Boot Camp (Up to 81% Off)",
                "url": "http://yipit.com/aff/eval/deal/?deal=RXk8HSAz&key=t5pm9EBw",
                "value": {
                    "formatted": "$124",
                    "raw": 124.00
                },
                "yipit_title": "Up to 81% Off at Fit Body Boot Camp",
                "yipit_url": "http://yipit.com/halifax-nova-scotia/groupon/10-day-fit-and-firm-program-or-four-week-rapid-fat-loss-program-at-fit-body-boot-camp-up-to-81-off-42/"
            }
        ]
    }
}

在PHP中,我需要接收id,lat,lan,name,url,yipit_url,yipit_id,img_bg, city, zip等,但我不知道如何在php页面上显示这些json对象。请帮帮我。

谢谢,Enamul

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-01-05 12:57:33

*“是的,我知道应该用json_decode来做,但我不知道怎么做。”*

好的,你以某种方式将数据放入一个变量,假设是$data

代码语言:javascript
复制
$array=json_decode($data, true);

我会将其转换为一个数组,在本例中,该数组的id中包含其他数组和变量。

查看整个print_r($array);并查看源代码,或echo '<pre>'; print_r($array);

因此,为了得到需要的位,您需要遍历数组,但需要的是在数组locations中的多个元素中的一个元素中,因此我们需要遍历它们

代码语言:javascript
复制
foreach ($array['response']['deals']['active']['locations'] as $location)
{
     echo 'id:'.$location['id'];
     echo 'lat':$location['lat'];
     //ect
}

或者你可以选择第一个

代码语言:javascript
复制
echo $array['response']['deals']['active']['locations'][0]['id'];
票数 1
EN

Stack Overflow用户

发布于 2013-01-05 12:48:44

http://php.net/manual/en/book.json.php

具体地说,您可能希望查看json_decode()

下面是一个小示例:

代码语言:javascript
复制
<?php

function JSONPrepare($json) {
    // This will convert ASCII/ISO-8859-1 to UTF-8.
    // Be careful with the third parameter (encoding detect list), because
    // if set wrong, some input encodings will get garbled (including UTF-8!)
    $input = mb_convert_encoding($json, 'UTF-8', 'ASCII,UTF-8,IS0-8859-1');

    // Remove UTF-8 BOM if present, json_decode() does not like it.
    if (substr($input, 0, 3) == pack("CCC", 0xEF, 0xBB, 0xBF)) {
        $input = substr($input, 0, 3);
    }

    return $input;
}

foreach (json_decode(JSONPrepare($my_json), true) as $k => $v) {
    // process the array here
}
?>
票数 4
EN

Stack Overflow用户

发布于 2013-01-05 12:56:32

json_decode会将json转换为PHP对象。

使用$response = json_decode($json);,其中$json是您在上面发布的JSON,然后:

代码语言:javascript
复制
echo $response->deals->business->id; // 11009
echo $response->deals->business->locations[0]->id; //974228 
echo $response->deals->ypit_url; // http://yipit.com/h...y-boot-camp-up-to-81-off-42/
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14168681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档