我真的很討厭'爲我做這個'的問題,但我完全喪失。我想我只是沒有得到JSON。因此,這裏是我的JSON的例子:JSON數組到JS對象數組
"max":"10",
"min":"0",
"attributes":[
{
"attributeName":"Fortitude",
"attributeColor":"#B7B7B7"
},
{
"attributeName":"Vigor",
"attributeColor":"#D5A6BD"
},
{
"attributeName":"Celerity",
"attributeColor":"#B4A7D6"
}
]
這是外部的,我想抓住它,然後設置一個js變量作爲屬性對象的數組。所以,如果在JS我設置:
var attributes = [];
attributes = whatEverNeedsToGoHere;
然後我遍歷該變量我可以這樣做:
console.log(attributes[0].attributeName);
並獲得「堅韌」。我知道如何使用$ .getJSON()獲取JSON和jQuery;但我不知道要將屬性數組轉換爲對象數組需要發生什麼。
更新:我現在正在調用JSON。
var attributesData = $.getJSON("jsonDB/attributes.js", function(data){
var thisAttribute = {"attributeName":String(data[i].attributeName),"attributeColor":String(data[i].attributeColor)};
attributes.push(thisAttribute);
console.log(attributes.attributeName);
});
「這是外部的,我想抓住它」 - 哪裏是你的Ajax調用,這是否? – 2015-04-02 14:43:42