简体   繁体   中英

Handling JSON response from cURL

I am trying to get data from an address verification API I use, and I'm having trouble getting data from it. I have no problem cURLing the addresses and getting a response, but I need help with PHP handling the response.

JSON structure:

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

My code:

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

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

But then I get the error of Trying to get property of non-object .

What am I doing wrong?

EDIT:

print_r($rd); gives me:

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 ) ) )

Invalid JSON:

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

should be:

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

Mind the closing " on the longitude value.

furthermore, the two entries need to be separated by , :

[
  { "input_index": 0,
    ...

  },


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

finally: what you get is an array of objects - not an object with arrays:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM