2012-08-13 229 views
0

我正在使用yahoo的placemaker从文本中提取位置名称。从这我得到一个回调函数,它给了我2种不同类型的数组。从不同数组中获取值javascript

这是我正在使用的代码,但我能够满足任何我想要的值。

Placemaker.getPlaces(text,function(o){ 
    if (typeof o.match!=='undefined' && o.match.length==1){ 
    latitude=o.match.place.centroid.latitude, longitude=o.match.place.centroid.longitude; 
    console.log(latitude,longitude);} 

    if(typeof o.match !=='undefined'&& o.match.length==2){ 
    latitude=o[match].place.centroid.latitude, 
    longitude=o[match].place.centroid.longitude; 
    console.log(latitude,longitude); 
     } 

第一阵列看起来像这样

({ 
    match: { 
     place: { 
      woeId: "29007292", 
      type: "Town", 
      name: "Jubila, West Bengal, IN", 
      centroid: { 
       latitude: "23.1626", 
       longitude: "87.7889" 
      } 
     }, 
     reference: [{ 
      woeIds: "29007292", 
      placeReferenceId: "2", 
      placeIds: "1", 
      start: "14", 
      end: "20", 
      isPlaintextMarker: "1", 
      text: "jubila", 
      type: "plaintext", 
      xpath: null 
     }, { 
      woeIds: "29007292", 
      placeReferenceId: "3", 
      placeIds: "1", 
      start: "82", 
      end: "88", 
      isPlaintextMarker: "1", 
      text: "jubila", 
      type: "plaintext", 
      xpath: null 
     }] 
    } 
}) 

,另一个这样

({ 
    match: [{ 
     place: { 
      woeId: "23424950", 
      type: "Country", 
      name: "Spain", 
      centroid: { 
       latitude: "39.8949", 
       longitude: "-2.98831" 
      } 
     }, 
     reference: { 
      woeIds: "23424950", 
      placeReferenceId: "1", 
      placeIds: "1", 
      start: "64", 
      end: "70", 
      isPlaintextMarker: "1", 
      text: "Espa\xF1a", 
      type: "plaintext", 
      xpath: null 
     } 
    }, { 
     place: { 
      woeId: "24865675", 
      type: "Continent", 
      name: "Europe", 
      centroid: { 
       latitude: "52.9762", 
       longitude: "7.85784" 
      } 
     }, 
     reference: { 
      woeIds: "24865675", 
      placeReferenceId: "2", 
      placeIds: "2", 
      start: "93", 
      end: "99", 
      isPlaintextMarker: "1", 
      text: "Europa", 
      type: "plaintext", 
      xpath: null 
     } 
    }] 
}) 

回答

0

变化

if (typeof o.match!=='undefined' && o.match.length==1){ 

if (typeof o.match!=='undefined'){ 

对于第二:

for(var i in o.match) { 
console.log(o.match[i].place.centroid.latitude); //wil show all lat's in loop 
} 

,或者如果你只想先lattitude:

console.log(o.match[0].place.centroid.latitude); //will show only first lat 
+0

是目前第一个是工作..你能告诉我第二阵列,以及因为这是最棘手的? – 2012-08-13 12:07:36

+0

它给了我这个错误:TypeError:o.match.place未定义 – 2012-08-13 12:17:23

+0

看到添加的代码,它应该是o.match [0] .place如果你想得到单一的格位,我测试过,它对我来说工作得很好 – 2012-08-13 12:18:47