我的PHP脚本specs.php输出以下内容:
{
"hd": {
"dimensions": [
"1920x1080",
"1920x1080",
"1920x1080"
],
"sizes": [
"603 KB",
"265 KB",
"438 KB"
]
},
"medium": {
"dimensions": [
"800x530",
"800x530",
"800x530"
],
"sizes": [
"198 KB",
"105 KB",
"152 KB"
]
},
"status": "success"
}
使用jQuery加载JSON并将其分配给specs_obj
我可以使用specs_obj访问第一个项目的"medium" "sizes",即specs_obj. medium.sizes[0]
如何在点符号表示法中使用变量?
var specs_obj;
$.post("specs.php", {},
function(data) {
if (data.status == "success") {
specs_obj = data;
writeSizes("medium");
} else {}
}, "json"
);
function writeSizes(preset) {
// test get medium dimensions from first file
var size = specs_obj. medium.sizes[0];
// var size = specs_obj.preset.sizes[0];
}