2014-06-22 3 views
0

내가 사용하는 주소 확인 API에서 데이터를 가져 오려고하고 있으며 데이터를 가져 오는 데 문제가 있습니다. 나는 주소를 cURLing하고 응답을 얻는 데 아무런 문제가 없지만 응답을 처리하는 PHP에 도움이 필요하다.cURL의 JSON 응답 처리

JSON 구조 :

[ 
    { "input_index": 0, 
    "candidate_index": 0, 
    "addressee": "Apple Inc", 
    "delivery_line_1": "1 Infinite Loop", 
    "delivery_line_2": "PO Box 42", 
    "last_line": "Cupertino CA 95014-2083", 
    "delivery_point_barcode": "950142083017", 
    "metadata": { 
     "latitude":"48.12685", 
     "longitude":"-39.16847" 
    }, 
    } 
    { "input_index": 1, 
    "candidate_index": 0, 
    "addressee": "Google Inc", 
    "delivery_line_1": "1600 Amphitheater Pkwy", 
    "delivery_line_2": "", 
    "last_line": "Mountain View CA 94043-1351", 
    "delivery_point_barcode": "950142083017", 
    "metadata": { 
     "latitude":"48.12685", 
     "longitude":"-39.16847" 
    }, 
    } 
] 

내 코드 :

$response = curl_exec($ch); 
curl_close($ch); 
$rd = json_decode($response); 

echo $rd->metadata[0]->latitude; 

그러나 나는 Trying to get property of non-object의 오류가 발생합니다.

내가 뭘 잘못하고 있니?

편집 :

인 print_r ($의 RD); 저를 제공합니다

Array ([0] => stdClass Object ([input_index] => 0 [candidate_index] 
=> 0 [addressee] => Randolph Gray [delivery_line_1] => 8758 Sunset Breeze Dr [last_line] => Reno NV 89506-4136 [delivery_point_barcode] 
=> 895064136585 [components] => stdClass Object ([primary_number] => 8758 [street_name] => Sunset Breeze [street_suffix] => Dr [city_name] 
=> Reno [state_abbreviation] => NV [zipcode] => 89506 [plus4_code] => 4136 [delivery_point] => 58 [delivery_point_check_digit] => 5) [metadata] => stdClass Object ([record_type] => S [zip_type] => Standard [county_fips] => 32031 [county_name] => Washoe [carrier_route] => C038 [congressional_district] => 02 [rdi] => Residential [elot_sequence] => 0381 [elot_sort] => A [latitude] => 
39.63353 [longitude] => -119.89745 [precision] => Zip9 [time_zone] => Pacific [utc_offset] => -8 [dst] => 1) [analysis] => stdClass Object ([dpv_match_code] => Y [dpv_footnotes] => AABB [dpv_cmra] => N [dpv_vacant] => N [active] => Y)) [1] => stdClass Object ([input_index] => 1 [candidate_index] => 0 [addressee] => Julio Valdez [delivery_line_1] => 7464 Gannon Dr [last_line] => Reno NV 89506-3186 [delivery_point_barcode] => 895063186644 [components] => stdClass Object ([primary_number] => 7464 [street_name] => Gannon [street_suffix] => Dr [city_name] => Reno [state_abbreviation] => NV [zipcode] => 89506 [plus4_code] => 3186 [delivery_point] => 64 [delivery_point_check_digit] => 4) [metadata] => stdClass Object ([record_type] => S [zip_type] => Standard [county_fips] => 32031 [county_name] => Washoe [carrier_route] => C038 [congressional_district] => 02 [rdi] => Residential [elot_sequence] => 0048 [elot_sort] => A [latitude] => 39.62646 [longitude] => -119.89913 [precision] => Zip9 [time_zone] => Pacific [utc_offset] => -8 [dst] => 1) [analysis] => stdClass Object ([dpv_match_code] => Y [dpv_footnotes] => AABB [dpv_cmra] => N [dpv_vacant] => N [active] => Y)) [2] => stdClass Object ([input_index] => 2 [candidate_index] => 0 [addressee] => Nicholas Carone [delivery_line_1] => 8685 Bagpipe Cir [last_line] => Reno NV 89506-4165 [delivery_point_barcode] => 895064165853 [components] => stdClass Object ([primary_number] => 8685 [street_name] => Bagpipe [street_suffix] => Cir [city_name] => Reno [state_abbreviation] => NV [zipcode] => 89506 [plus4_code] => 4165 [delivery_point] => 85 [delivery_point_check_digit] => 3) [metadata] => stdClass Object ([record_type] => S [zip_type] => Standard [county_fips] => 32031 [county_name] => Washoe [carrier_route] => C038 [congressional_district] => 02 [rdi] => Residential [elot_sequence] => 0227 [elot_sort] => A [latitude] => 
39.63367 [longitude] => -119.89965 [precision] => Zip9 [time_zone] => Pacific [utc_offset] => -8 [dst] => 1) [analysis] => stdClass Object ([dpv_match_code] => Y [dpv_footnotes] => AABB [dpv_cmra] => N [dpv_vacant] => N [active] => Y))) 
+0

'print_r ($ rd)'당신에게 줄래? – bloodyKnuckles

+0

또한 다음과 같이 접근해야합니다 :'$ rd [0] -> metadata-> latitude;'. 배열의 객체가 있습니다 –

+0

@RahilWazir 잘못된 JSON이 수정되었습니다. 오타 일 수도 있습니다. – JakeGould

답변

0

잘못된 JSON :

"metadata": { 
    "latitude":"48.12685", 
    "longitude":"-39.16847 
} ... 

가 있어야한다 :

"metadata": { 
    "latitude":"48.12685", 
    "longitude":"-39.16847" 
} ... 

마음 경도 값에 닫는 ".

또한, 두 항목은 ,으로 구분해야합니다 마지막으로

[ 
    { "input_index": 0, 
    ... 

    }, 


    { "input_index": 1, 
    ... 
    } 
] 

: 당신이 얻을 것은 객체의 배열입니다 -하지 배열로 객체 : 무엇을

$rd[0]->metadata->latitude ... 
+0

나는 그 부분이 나의 불행일지도 모른다라고 생각한다, 나는 응답을 점검 할 것이다. – user3746522

+0

그것은이었다. 위도도 경도도 따옴표로 묶지 않습니다 ... – user3746522

+0

http://smartystreets.com/kb/liveaddress-api/parsing-the-response – user3746522