我如何在PHP中解析像geo:point的JSON?

时间:2013-01-16 12:03:51

标签: php json parsing

  

可能重复:
  How to access an object property with a minus-sign?

我正在尝试解析geo:point / geo:lat但无法快速解决问题。这是我到目前为止的代码

  $content = get_data('http://ws.audioscrobbler.com/2.0/?method=geo.getevents&location=united+kingdom&api_key=XXX&format=json&limit=10000&festivalsonly=1');
  $LFMdata = json_decode($content);
  foreach ($LFMdata->events->event as $event) {
      $venue_lat = $event->venue->location["geo:point"]["geo:lat"];
      $venue_long = $event->venue->location["geo:point"]["geo:long"];

JSON将包含类似

的内容
"geo:point": {
     "geo:lat": "52.7352",
     "geo:long": "-1.695392"
}

任何人都有任何想法?在JavaScript中看到的例子,但不是PHP

1 个答案:

答案 0 :(得分:2)

您可以使用

$venue_lat = $event->venue->location->{"geo:point"}->{"geo:lat"};

或者让它像关联数组一样返回所有内容(json_decode的第二个参数设置为true):

 $LFMdata = json_decode($content, true);
  foreach ($LFMdata["events"]["event"] as $event) {
      $venue_lat = $event["venue"]["location"]["geo:point"]["geo:lat"];
      $venue_long = $event["venue"]["location"]["geo:point"]["geo:long"];