2011-05-24 109 views
0

JSON結果標記和信息框:JSON結果對谷歌地圖

 
[{"Company":"Company1","Country":"US","Phone":"209-555-8400","Website":"www.mywebsite.com","Latitude":35.782,"Longitude":-120.2269}, 
{"Company":"Company2","Country":"US","Phone":"909-555-5500","Website":"www.mywebsite.com","Latitude":36.112782,"Longitude":-111.52691}, 
{"Company":"Company3","Country":"US","Phone":"702-555-5200","Website":"www.mywebsite.com","Latitude":37.0427,"Longitude":-112.1818}, 
{"Company":"Company4","Country":"US","Phone":"602-555-5600","Website":"www.mywebsite.com","Latitude":38.4369,"Longitude":-113.8671}, 
{"Company":"Company5","Country":"US","Phone":"800-555-4716","Website":"www.mywebsite.com","Latitude":39.244946,"Longitude":-114.211941}] 

的Javascript中查看

 

     var locations = ["How can I get my JSON results in here"] 

     var map = new google.maps.Map(document.getElementById('map_canvas'), { 
      zoom: 2, 
      center: new google.maps.LatLng(40.0, -35.0), 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }); 

     var infowindow = new google.maps.InfoWindow(); 

     var marker, i; 

     for (i = 0; i 

我需要的是這樣的:

 
var locations = ['Company1 209-555-8400 US www.mywebsite.com ', 35.78, -120.23] 

回答

0

看一看將json解析爲一個對象。類似JSON.net會很有用。

0

你可以使用jQuery解析器

var dataJson = $.parseJSON(data); 

那麼你遍歷數組

for(var i=0;i<dataJson.length;i++) 
{ 
    var company = dataJson[i].Company; 
    .... 
} 

相反,如果你想上面的準備串,你可以將它設置在服務器端

Type myType = myObject.GetType(); 
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties()); 
String.Join(" ", props.select(p => p.GetValue(yourObject,null))) 

簡明的方式

String.Join(" ", (List<PropertyInfo>)(object.GetType().GetProperties()).select(p => p.GetValue(yourObject,null)))