2013-09-23 67 views
0

我有這樣的json格式 -Json數據提取使用jquery

我怎樣才能得到照片的參考價值?我嘗試了很多。

{ 
"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

顯示你已經嘗試過的東西。 –

+0

這應該是因爲我已經成功地獲得經度,經度和附近的價值。** $。each(data.results,function(key,value){alert.value.photos [0] .photo_reference); } ** –

+0

這應該工作,有沒有在您的JavaScript控制檯中的任何錯誤?什麼是警報顯示? –

回答

2

照片參考是在一個數組中,因此您需要循環播放「結果」數組,然後循環播放照片數組(假設可能有多個),然後您將可以訪問「照片參考「。

var data = { JSON HERE } 
$.each(data.results,function() { 
$.each(this.photos,function() { 
    console.log(this.photo_reference); 
}); 
}) 
+0

那裏是一個錯誤,這是TypeError:e是undefined http:,../jquery-1.9.1.min.js第3行 –

+0

JSON缺少一個關閉陣列支架],我糾正了這一點,然後我在Firebug中運行了這個程序,並且能夠訪問所有照片引用 – nathanfirth

+0

那麼我能做些什麼呢? –

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); 
    } 
} 
0

首先檢查您的json是否有效,然後嘗試以下內容(pre-condit離子: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); 
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); 
} 

,這應該工作,當然是有一些調整