jsonObject到js数组

时间:2013-05-30 17:23:04

标签: javascript jquery json

所以我有一个json对象,我需要一个包含该数组数据的数组,如下所示:

['city 1','city 2','city ..']

所以这是json输出:

{"\u0000*\u0000_data":[{"id":"298506","Country":"br","City":"fazenda marrecas","AccentCity":"Fazenda Marrecas","Region":"06","Population":"0","Latitude":"-3.4","Longitude":"-39.883333"},
{"id":"305783","Country":"br","City":"marecas","AccentCity":"Marecas","Region":"04","Population":"0","Latitude":"-4.820556","Longitude":"-62.603889"},{"id":"305960","Country":"br","City":"marrecas","AccentCity":"Marrecas","Region":"05","Population":"0","Latitude":"-10.6","Longitude":"-42.65"},
{"id":"305961","Country":"br","City":"marrecas","AccentCity":"Marrecas","Region":"06","Population":"0","Latitude":"-6.133333","Longitude":"-40.483333"},{"id":"305962","Country":"br","City":"marrecas","AccentCity":"Marrecas","Region":"17","Population":"0","Latitude":"-7.1","Longitude":"-37.666667"},{"id":"305963","Country":"br","City":"marrecas","AccentCity":"Marrecas","Region":"18","Population":"0","Latitude":"-25.116667","Longitude":"-51.4"},{"id":"305964","Country":"br","City":"marrecas","AccentCity":"Marrecas","Region":"20","Population":"0","Latitude":"-5.633333","Longitude":"-41.75"},{"id":"305965","Country":"br","City":"marrecas","AccentCity":"Marrecas","Region":"21","Population":"0","Latitude":"-21.933333","Longitude":"-41.066667"},{"id":"305966","Country":"br","City":"marrecas","AccentCity":"Marrecas","Region":"27","Population":"0","Latitude":"-21.35","Longitude":"-51.866667"},{"id":"305967","Country":"br","City":"marrecas","AccentCity":"Marrecas","Region":"28","Population":"0","Latitude":"-9.95","Longitude":"-37.533333"},{"id":"305968","Country":"br","City":"marrecas mulaco","AccentCity":"Marrecas Mulaco","Region":"21","Population":"0","Latitude":"-21.933333","Longitude":"-41.066667"},{"id":"311674","Country":"br","City":"porto das marrecas","AccentCity":"P\u00f4rto das Marrecas","Region":"27","Population":"0","Latitude":"-21.35","Longitude":"-51.866667"},{"id":"311941","Country":"br","City":"porto marrecas","AccentCity":"P\u00f4rto Marrecas","Region":"27","Population":"0","Latitude":"-21.35","Longitude":"-51.866667"},{"id":"316693","Country":"br","City":"sao marecas","AccentCity":"S\u0103o Marecas","Region":"23","Population":"0","Latitude":"-28.966667","Longitude":"-51.066667"},{"id":"322064","Country":"br","City":"sitio lagoa das marrecas","AccentCity":"S\u00edtio Lagoa das Marrecas","Region":"06","Population":"0","Latitude":"-6.25","Longitude":"-39.883333"},{"id":"857141","Country":"es","City":"astorecas","AccentCity":"Astorecas","Region":"59","Population":"0","Latitude":"43.278765","Longitude":"-2.772733"},{"id":"878632","Country":"es","City":"recas","AccentCity":"Recas","Region":"54","Population":"0","Latitude":"40.052855","Longitude":"-3.990899"},{"id":"879169","Country":"es","City":"robledo de sobrecastro","AccentCity":"Robledo de Sobrecastro","Region":"55","Population":"0","Latitude":"42.415593","Longitude":"-6.739204"},{"id":"932863","Country":"fr","City":"entrecasteaux","AccentCity":"Entrecasteaux","Region":"B8","Population":"0","Latitude":"43.517309","Longitude":"6.240614"},{"id":"993698","Country":"gb","City":"harecastle","AccentCity":"Harecastle","Region":"N1","Population":"0","Latitude":"53.066667","Longitude":"-2.25"},{"id":"1002394","Country":"gb","City":"trecastle","AccentCity":"Trecastle","Region":"Y8","Population":"0","Latitude":"51.933333","Longitude":"-3.616667"},{"id":"1311553","Country":"ie","City":"clarecastle","AccentCity":"Clarecastle","Region":"03","Population":"0","Latitude":"52.8155556","Longitude":"-8.9666667"},{"id":"1505435","Country":"it","City":"boscotrecase","AccentCity":"Boscotrecase","Region":"04","Population":"10393","Latitude":"40.766667","Longitude":"14.466667"},{"id":"1522562","Country":"it","City":"trecasali","AccentCity":"Trecasali","Region":"05","Population":"0","Latitude":"44.933333","Longitude":"10.266667"},{"id":"1522563","Country":"it","City":"trecastagni","AccentCity":"Trecastagni","Region":"15","Population":"0","Latitude":"37.616667","Longitude":"15.083333"},{"id":"1900274","Country":"mx","City":"rancho almirecas","AccentCity":"Rancho Almirecas","Region":"06","Population":"0","Latitude":"28.366667","Longitude":"-107.2"},{"id":"2320612","Country":"ro","City":"colonia recas","AccentCity":"Colonia Recas","Region":"36","Population":"0","Latitude":"45.8","Longitude":"21.5"},{"id":"2334967","Country":"ro","City":"recas","AccentCity":"Recas","Region":"36","Population":"8508","Latitude":"45.798889","Longitude":"21.500833"}],"\u0000*\u0000_table":{},"\u0000*\u0000_connected":true,"\u0000*\u0000_tableClass":"City","\u0000*\u0000_rowClass":"Zend_Db_Table_Row","\u0000*\u0000_pointer":0,"\u0000*\u0000_count":28,"\u0000*\u0000_rows":[],"\u0000*\u0000_stored":true,"\u0000*\u0000_readOnly":false}

Pastebin ex,更好的缩进:http://pastebin.com/kEfBSNX5

这是我目前的代码:

    $.each(data._data, function(i, obj) {
        locations.push([obj.City, obj.Latitude, obj.Longitude]);
    });

错误:

Uncaught TypeError: Cannot read property 'length' of undefined 

// LE

  success: function(data)
  {
     var locations = [];
    $.each(data,function(k,v){
        $.each(v,function(key,value){
            locations.push(value['City']); 
        }); 
    });
        console.log(locations);
        return locations;
  }
});

1 个答案:

答案 0 :(得分:0)

请查看此fiddle

您是否尝试获取阵列中的所有城市值。

var city = [];
$.each(data,function(k,v){
    $.each(v,function(key,value){
        city.push(value['City']); 
    }); 
    alert(city)
});

我在小提琴例子中做到了这一点。 让我知道这是你的要求。