我需要从json文件中获取一个数组,我不知道该怎么做。
这是我想要从中获取数组的代码:
$.getJSON('saveGames/userID' + userID + '_SAVEALPHA.json', function(data) {
console.log("Save data from: userID" + userID + "_SAVEALPHA.json: " + data);
var testVar = jQuery.parseJSON(data);
var pokemonAryTest = testVar[0].pokemon;
pokemonAry = [pokemonAryTest];
console.log("loaded players Pokemon: " + pokemonAry);
console.log(pokemonAry[0])
});
我尝试将pokemonAry
的索引更改为1并返回undefined。当我保持索引相同时,它返回["Pikachu, Charmander"]
,所以我认为它的表现就好像它是一个字符串。
这里是.json:
"[{\"userID\":\"1\",\"saveName\":\"g\",\"pokemon\":[\"Pikachu, Charmander\"]}]"
答案 0 :(得分:0)
当您使用getJSON()
方法时,无需解析JSON
数据,jQuery会处理它。 :)
$.getJSON('saveGames/userID' + userID + '_SAVEALPHA.json', function(data) {
console.log("Save data from: userID" + userID + "_SAVEALPHA.json: " + data);
var pokemonAry = data[0].pokemon;
console.log("loaded players Pokemon: " + pokemonAry);
console.log(pokemonAry[0])
});