0

我有这样的json格式-

我怎样才能得到照片的参考价值?我已经尝试了很多。

{
"debug_info" : [],
"html_attributions" : [
   "Listings by \u003ca href=\"http://www.yellowpages.com.au/\"\u003eYellow Pages\u003c/a\u003e"
],
    "results" : [
  {
     "geometry" : {
        "location" : {
           "lat" : -33.859618,
           "lng" : 151.208017
        }
     },
     "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png",
     "id" : "4eb1c6eb8ec71a621f4d88e9aef77b0fce7ac304",
     "name" : "Harbour Rocks Hotel Sydney",
     "photos" : [
        {
           "height" : 196,
           "html_attributions" : [],
           "photo_reference" : "CoQBeAAAAKTzIvsp1PJml8nzzxCAggrnyEu1-J3B6YbWhf3SMJLadZODgKnkEvWuXwKemOM4xc0IQpBv3LmkPOOZvXEuM0PBNp6YfvQc1EOHR_UF9K87ITUfM4f1nCnTFWpWjXFY5J4Aw1z1hJdu0LGfqcszeaImXBRxdzxlUw0Z9QTK-wWOEhCrs6v7gxJVLymtHl0WGv33GhTq_DgXtofepJ909JqOFrXw90fOEQ",
           "width" : 294
        }
     ],
     "rating" : 3.7,
     "reference" : "CoQBeAAAAHzr_LYm87OynYHffSsOFLsqzQO3iWo_DrC-JE8oZUNvYwyAlzumN0F9S87Lb2AbZrejbpbzvGC0JEy1-R52WbME4sRfqOYpM1AAvTRXVjMLus6ZRjU0nnJOGxyJdEdPRcPURThiTMDqH2AB9p4cGyqLtdKdG8hSNTcVOBykIAt7EhD6quPd7PCrVy4aETt8lYarGhRWuRNfSsnzsEdJ2MecTUXV0F65uQ",
     "types" : [ "cafe", "bar", "lodging", "food", "establishment" ],
     "vicinity" : "34 Harrington Street, The Rocks"
  },
  {
     "geometry" : {
        "location" : {
           "lat" : -33.87054,
           "lng" : 151.198815
        }
     },
     "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png",
     "id" : "c71365287e7606bd21a3311d21fda087830b7813",
     "name" : "Pancakes on the Rocks",
     "opening_hours" : {
        "open_now" : false
     },
     "photos" : [
        {
           "height" : 1224,
           "html_attributions" : [
              "\u003ca href=\"https://plus.google.com/105663944571530352563\"\u003eJoshua Gilmore\u003c/a\u003e"
           ],
           "photo_reference" : "CnRoAAAAj1WWQDr0Uh-Naj8Fzg413dzP-3mix2O53r9mZEvRUaMEWZGiHthrE-whJhuW-aIUhyL-yk47DXOXR1DKHD5UacOzi99xTjSCNLXN-5_xetw_9xyRZkLofzamziEFoijl2v_JPthE46BoZRl6fQmeaBIQewa4UNPWTksaCfBBgckw-hoUBFtYJ87HMq2ZrCGPRhW0euefWYc",
           "width" : 1632
        }
     ],
     "price_level" : 2,
     "rating" : 3.8,
     "reference" : "CoQBcgAAAAcnHQk8ynZuToE6HAMJIRklS06ldx7XJXv5AhKQgIgXLURw71KoI_u3bAZ6Fv5X_LUv0QdTX_hQIwZpdLtegQvHOyIFKSRjeKw7_G-cdC7Ly_mJAzB-fXHhJlgKmHTFZ8J-WdK5ZjU7mm9ABBG0Q4rvoN5vyAWGfYGYX3JpKDmgEhA-s27w07KdpRZ7wLoaQKhZGhQdXJoNOqN1wuu0RC_f3c--EjUnGg",
     "types" : [ "cafe", "restaurant", "food", "establishment" ],
     "vicinity" : "Harbourside Shopping Centre,Darling Harbour,227/229-230 Darling Drive, Sydney"
  },
.
.
.

}

提前致谢。

4

4 回答 4

2

照片参考在一个数组中,因此您需要遍历“结果”数组,然后遍历照片数组(假设可能有多个),然后您就可以访问“照片参考”。

var data = { JSON HERE }
$.each(data.results,function() {
 $.each(this.photos,function() {
    console.log(this.photo_reference);
 });
})
于 2013-09-23T18:10:43.837 回答
0

首先检查您的 json 是否有效,然后尝试以下操作(前提条件:photo_reference 不是数组)

$jsonIterator = new RecursiveIteratorIterator (
    new RecursiveArrayIterator(json_decode($yourJsonString,TRUE)),
    RecursiveIteratorIterator::SELF_FIRST
);

$result = array();
$i = 0;

foreach ($jsonIterator as $key => $val) {
    if ($key == 'photo_reference')
        $result[$i++] = $val;
}

var_dump($result);
于 2013-09-23T18:55:23.830 回答
0

这是我在我们的 api 中使用 json 的内容。我只是使用 jquery 来解析 json。从那里把它当作一个对象 var whatIneed = Obj.Variable

for (var i = 0; i < jsonList.length; i++) {
    var photojson = $.parseJSON(jsonList[i]);
    var photo = new Object();
    photo.title = event.title;
    photo.id = event.id;
    ...
    photolist.push(ev);
}

这应该可以工作,当然还有一些调整

于 2013-09-23T19:13:29.270 回答
0

假设这个 JSON 对象位于一个名为的 javascript 变量中,placesJSON您可以像这样访问第一个地图结果:

placesJSON.results[0].photos[0].photo_reference

要在所有结果中获取每个 photo_reference,您应该像这样遍历结果:

for(var placeIndex in placesJSON.results){
  for(var photoIndex in placesJSON.results[placeIndex].photos) {
    var reference = placesJSON.results[placeIndex].photos[photoIndex].photo_reference;
    // Do something with the reference here
    console.log(reference);
  }
}
于 2013-09-23T18:12:22.167 回答