数组中的Json键值对

时间:2013-02-25 04:12:00

标签: json

尝试使用jquery ui中的自动完成功能,但我认为我的数组错误,因为它包含位置的id

var locations = [{"182":{"id":"182","name":"England"},"42":{"id":"42","name":"Barnsley"},"6":{"id":"6","name":"Bath"},"155":{"id":"155","name":"Beverley"},"7":{"id":"7","name":"Birmingham"},"72":{"id":"72","name":"Blackburn"},"8":{"id":"8","name":"Blackpool"},"69":{"id":"69","name":"Bournemouth"},"45":{"id":"45","name":"Bradford"},"38":{"id":"38","name":"Bridlington"},"78":{"id":"78","name":"Brighton"},"9":{"id":"9","name":"Bristol"},"73":{"id":"73","name":"Burnley"},"160":{"id":"160","name":"Colchester"},"11":{"id":"11","name":"Cornwall"},"12":{"id":"12","name":"Coventry"},"44":{"id":"44","name":"Cumbria"},"13":{"id":"13","name":"Derby"},"14":{"id":"14","name":"Devon"},"161":{"id":"161","name":"Dewsbury"},"40":{"id":"40","name":"Doncaster"},"156":{"id":"156","name":"Driffield"},"15":{"id":"15","name":"Essex"},"75":{"id":"75","name":"Exeter"},"16":{"id":"16","name":"Gloucestershire"},"46":{"id":"46","name":"Goole"},"17":{"id":"17","name":"Grimsby"},"71":{"id":"71","name":"Guildford"},"74":{"id":"74","name":"Hartlepool"},"54":{"id":"54","name":"Hornsea"},"154":{"id":"154","name":"Howden"},"1":{"id":"1","name":"Hull"},"18":{"id":"18","name":"Kent"},"76":{"id":"76","name":"Lanarkshire"},"5":{"id":"5","name":"Leeds"},"19":{"id":"19","name":"Leicestershire"},"20":{"id":"20","name":"Lincolnshire"},"21":{"id":"21","name":"Liverpool"},"43":{"id":"43","name":"London"},"23":{"id":"23","name":"Manchester"},"67":{"id":"67","name":"Middlesborough"},"68":{"id":"68","name":"Milton Keynes"},"24":{"id":"24","name":"Newcastle"},"51":{"id":"51","name":"Northampton"},"25":{"id":"25","name":"Nottingham"},"77":{"id":"77","name":"Plymouth"},"65":{"id":"65","name":"Pontefract"},"41":{"id":"41","name":"Portsmouth"},"36":{"id":"36","name":"Rotherham"},"39":{"id":"39","name":"Scarborough"},"26":{"id":"26","name":"Scunthorpe"},"27":{"id":"27","name":"Sheffield"},"28":{"id":"28","name":"Somerset"},"29":{"id":"29","name":"Stoke On Trent"},"66":{"id":"66","name":"Sunderland"},"31":{"id":"31","name":"Tamworth"},"32":{"id":"32","name":"Warwick"},"153":{"id":"153","name":"Withernsea"},"33":{"id":"33","name":"Wolverhampton"},"34":{"id":"34","name":"Worcestershire"},"37":{"id":"37","name":"York"},"189":{"id":"189","name":"France"},"167":{"id":"167","name":"Bordeaux"},"163":{"id":"163","name":"Lyon"},"164":{"id":"164","name":"Marseille"},"169":{"id":"169","name":"Nice"},"162":{"id":"162","name":"Paris"},"168":{"id":"168","name":"Rennes"},"165":{"id":"165","name":"Strasbourg"},"166":{"id":"166","name":"Tours"},"191":{"id":"191","name":"Germany"},"178":{"id":"178","name":"Berlin"},"179":{"id":"179","name":"Frankfurt"},"180":{"id":"180","name":"Hamburg"},"181":{"id":"181","name":"Munich"},"188":{"id":"188","name":"India"},"151":{"id":"151","name":"Agra"},"133":{"id":"133","name":"Ahmedabad"},"131":{"id":"131","name":"Bangalore"},"144":{"id":"144","name":"Bhopal"},"134":{"id":"134","name":"Chennai"},"130":{"id":"130","name":"Delhi"},"149":{"id":"149","name":"Ghaziabad"},"132":{"id":"132","name":"Hyderabad"},"142":{"id":"142","name":"Indore"},"138":{"id":"138","name":"Jaipur"},"140":{"id":"140","name":"Kanpur"},"135":{"id":"135","name":"Kolkata"},"139":{"id":"139","name":"Lucknow"},"150":{"id":"150","name":"Ludhiana"},"129":{"id":"129","name":"Mumbai"},"141":{"id":"141","name":"Nagpur"},"152":{"id":"152","name":"Nashik"},"147":{"id":"147","name":"Patna"},"146":{"id":"146","name":"Pimpri-Chinchwad"},"137":{"id":"137","name":"Pune"},"136":{"id":"136","name":"Surat"},"143":{"id":"143","name":"Thane"},"148":{"id":"148","name":"Vadodara"},"145":{"id":"145","name":"Visakhapatnam"},"183":{"id":"183","name":"Ireland"},"63":{"id":"63","name":"Cork"},"52":{"id":"52","name":"Dublin"},"64":{"id":"64","name":"Limerick"},"190":{"id":"190","name":"Italy"},"177":{"id":"177","name":"Florence"},"176":{"id":"176","name":"Genoa"},"171":{"id":"171","name":"Milan"},"175":{"id":"175","name":"Palermo"},"170":{"id":"170","name":"Rome"},"174":{"id":"174","name":"Turin"},"184":{"id":"184","name":"Northern Ireland"},"61":{"id":"61","name":"Armagh"},"3":{"id":"3","name":"Belfast"},"62":{"id":"62","name":"Lisburn"},"59":{"id":"59","name":"Londonderry"},"60":{"id":"60","name":"Newry"},"55":{"id":"55","name":"Newtownards"}}]

我从数组的开头和结尾错过了[和],但我现在意识到我需要一个能够格式化多维数组的函数

这是我的函数,适用于非多维数组

function imAutocompleteJSONParse(data) {
var rows = [];
var rowData = null;
var dataLength = data.length;
for (var i = 0; i < dataLength; i++) {
    rowData = data[i];

    rows[i] = {
        label: rowData.name,
        value: rowData.id
    };


}
return rows;

}

如何在此功能中访问id和name?

感谢

1 个答案:

答案 0 :(得分:0)

不需要$.parseJSON(locations),因为 locations不是JSON。它已经是一个合适的JavaScript对象。

$('#location_id').autocomplete({source: locations});