dongshijiao6890 2013-01-05 04:46 采纳率: 0%
浏览 36
已采纳

如何从php接收json数据? [关闭]

I am trying to work with an API. Which returns json. below is the output:

{
    "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/"
            }
        ]
    }
}

In PHP I need to receive id,lat,lan,name,url,yipit_url,yipit_id,img_bg, city, zip etc. But I don't know how to show those json object on a php page. Please help me.

Thanks, Enamul

  • 写回答

4条回答 默认 最新

  • duanniu3385 2013-01-05 04:57
    关注

    *"Yeah I know it should be done with json_decode. But I don't know how to do it."*

    Ok, somehow you're getting data into a variable, let's say $data

    $array=json_decode($data, true);
    

    will convert that into an array which in this case has other arrays and variables inside id

    To see the whole thing print_r($array); and view source code, or echo '<pre>'; print_r($array);

    So to get to the bits you want to need to traverse the array, but what you want is in one element of many in an array locations so we need to iterate through them

    foreach ($array['response']['deals']['active']['locations'] as $location)
    {
         echo 'id:'.$location['id'];
         echo 'lat':$location['lat'];
         //ect
    }
    

    or you could pick the first one

    echo $array['response']['deals']['active']['locations'][0]['id'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)