程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> 遍歷-json轉換問題,求解求搭救。。。。。。。。。

遍歷-json轉換問題,求解求搭救。。。。。。。。。

編輯:編程綜合問答
json轉換問題,求解求搭救。。。。。。。。。

[
{
wFrom: {
fLatitude: "114.057868",
fLongitude: "22.543099",
fName: "深圳市"
},
wTo: {
tLatitude: "37.6173",
tLongitude: "55.755826",
tName: "Moscow"
},
wValue: "31",
wkey: "深圳市",
wproperties: "bag"
},
{
wFrom: {
fLatitude: "116.681972",
fLongitude: "23.354091",
fName: "汕頭市"
},
wTo: {
tLatitude: "37.6173",
tLongitude: "55.755826",
tName: "Moscow"
},
wValue: "24",
wkey: "汕頭市",
wproperties: "Baby Toys"
}
];
上面個數組怎麼遍歷構造出下面這種結構的數據:
var geodata = { "Moscow":[37.6173,55.755826],
"深圳市":[114.057868,22.543099],
"汕頭市":[116.681972,23.354091] };
求高手指點啊

最佳回答:


 <!DOCTYPE html>
<html>
<body>

<script>
var json=[ 
{ 
wFrom: { 
fLatitude: "114.057868", 
fLongitude: "22.543099", 
fName: "深圳市" 
}, 
wTo: { 
tLatitude: "37.6173", 
tLongitude: "55.755826", 
tName: "Moscow" 
}, 
wValue: "31", 
wkey: "深圳市", 
wproperties: "bag" 
}, 
{ 
wFrom: { 
fLatitude: "116.681972", 
fLongitude: "23.354091", 
fName: "汕頭市" 
}, 
wTo: { 
tLatitude: "37.6173", 
tLongitude: "55.755826", 
tName: "Moscow" 
}, 
wValue: "24", 
wkey: "汕頭市", 
wproperties: "Baby Toys" 
} 
];
function get(){
   var obj;
   var wFrom;
   var wTO;
   var geodata ={};
   for(var i=0;i<json.length;i++){
      obj = json[i];
      wFrom = obj['wFrom'];
      wTO = obj['wTo'];
      geodata[obj.wkey]=[wFrom.fLatitude,wFrom.fLongitude];
      geodata[wTO.tName]=[wTO.tLatitude,wTO.tLongitude];
   }
   return geodata;
}
var geodata  = get();
alert(geodata );
</script>


</body>
</html>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved