处理来自cURL的JSON响应

时间:2014-06-22 17:24:00

标签: php json curl

我正在尝试从我使用的地址验证API获取数据,但我无法从中获取数据。我没有问题地址和获得响应,但我需要帮助PHP处理响应。

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

1 个答案:

答案 0 :(得分: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 ...