2010-06-07 41 views
1

JSON數據我有這樣的JSON數據:我如何使用這像數組

{"Adobe Photoshop":"70","Adobe Premier":"71","Animasyon":"72","AutoCAD":"73","Bro\u015f\u00fcr & Bilet":"75","CorelDraw":"76","Flash":"77","Grafik Tasar\u0131m":"68","Karikat\u00fcr":"78","Kartvizit Tasar\u0131m":"79","Logo Tasar\u0131m":"80","Poster":"81","Reklam Tasar\u0131m":"74","Ses D\u00fczenleme":"82","Video D\u00fczenleme":"69","Web Tasar\u0131m":"83"} 

首先想到(Adobe公司的Photoshop)是指標籤名,第二件事情(70)是指標籤ID。我想在像jQuery這樣的數組中使用這些信息來爲每個標記構建複選框。我怎麼做到這一點?由於

回答

1

像這樣的事情?

http://jsfiddle.net/LKNKB/

var data = {"Adobe Photoshop":"70","Adobe Premier":"71","Animasyon":"72","AutoCAD":"73","Bro\u015f\u00fcr & Bilet":"75","CorelDraw":"76","Flash":"77","Grafik Tasar\u0131m":"68","Karikat\u00fcr":"78","Kartvizit Tasar\u0131m":"79","Logo Tasar\u0131m":"80","Poster":"81","Reklam Tasar\u0131m":"74","Ses D\u00fczenleme":"82","Video D\u00fczenleme":"69","Web Tasar\u0131m":"83"}; 

var $div = $('<div/>'); 

for(var name in data) { 
    $('<input/>',{ type:"checkbox", value:data[name] }).appendTo($div); 
    $('<label/>',{ text:name }).appendTo($div); 
    $('<br/>').appendTo($div); 
} 

$div.appendTo('body');​ 
3

你正在尋找一個for in循環,就像這樣:

for(var key in something) { 
    if (!something.hasOwnProperty(key)) continue; //Skip inherited members 

    var value = something[key]; 
    //Do things... 
} 

注意,迭代順序沒有定義。

1

你想要一個for...in聲明:

var i; 
for (i in hash) { 
    doSomething(i, hash[i]); 
}