我有这样的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"
},
.
.
.
}
提前致谢。
答案 0 :(得分:2)
照片参考是一个数组,所以你需要循环“结果”数组,然后循环遍历照片数组(假设可能有多个),然后你就可以访问“照片参考”。
var data = { JSON HERE }
$.each(data.results,function() {
$.each(this.photos,function() {
console.log(this.photo_reference);
});
})
答案 1 :(得分:0)
假设此JSON对象位于名为placesJSON
的javascript变量中,您可以像这样访问第一个地图结果:
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);
}
}
答案 2 :(得分: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);
答案 3 :(得分:0)
这是我在使用json的api中使用的内容..我只是使用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);
}
这应该可行,当然还有一些调整