2013-01-05 4 views
0

API를 사용하려고합니다. 어느 것이 json을 반환합니다. 아래의 출력은 다음과 같습니다 PHP에서php에서 json 데이터를받는 방법은 무엇입니까?

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

나는 id,lat,lan,name,url,yipit_url,yipit_id,img_bg, city, zip 등을받을 필요하지만이 PHP 페이지에 그 json 개체를 표시하는 방법을 모르겠어요. 도와주세요.

감사합니다, Enamul

특히
+1

어, json_decode는 어떨까요? http://php.net/manual/en/function.json-decode.php – sberry

+0

그래, json_decode로해야한다는 것을 알고있다. 그러나 나는 그것을 어떻게하는지 모른다. – user1559230

답변

1

* ".. 나는 그것이 json_decode으로 수행해야하지만 그것을하는 방법을 모른다 알고 그래" *

좋아, 당신이 변수로 데이터를 받고있어 어떻게 든,의는 $data

$array=json_decode($data, true); 

이 경우 다른 배열을 가지고 배열로 그 변환합니다 가정 해 봅시다 및 ID

내부 변수는 모든 일 print_r($array); 볼 소스 코드를 참조하거나하려면 echo '<pre>'; print_r($array);

그래서 당신은 배열을 통과해야 할 비트에 도착,하지만 당신이 원하는 것은 많은 사람들의 하나 개의 요소에 배열에 locations 그래서 우리는 그들을 통해

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

을 반복하는 것을 필요로하거나 선택할 수있는 최초의 하나

echo $array['response']['deals']['active']['locations'][0]['id']; 
+0

작동하지 않았습니다. 정의되지 않은 인덱스 : 활성 – user1559230

+0

나는 눈이 멀어지고있다. 그것을 디버그하십시오. – Popnoodles

4

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

당신이 json_decode()

에서 작은 예를 볼 수도 있습니다 :

<?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 
} 
?> 
0

json_decode는 PHP 오브젝트로 JSON 변환됩니다 . $ json으로 당신이 다음, 위의 게시 한 JSON은 $response = json_decode($json);를 사용

:

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

당신은 객체로 해석하기 쉬운 배열로 그 데이터를 얻을 수 있습니다.

$parsedData = json_decode($jsondata, 1); 

print_r ($ parsedData);

관련 문제