我有什么......
$.getJSON('ui-DashboardWidgetsGet.php', function(msg)
{
alert(msg);
if(msg.error == "yes"){console.log('Error Found: '+ msg.errorMsg);}
else
{
ztsDashboardJSON = msg;
}
});
var ztsDashboardJSONCount = ztsDashboardJSON.widgets[0].length;
转储
{ "widgets": [{ "column1": [ {"weight": 1, "bID": 1, "hideMe": false, "collapse": false, "titleOf": "Test 1", "colorOf": "color-yellow", "theFunction": "functionName"}, {"weight": 2, "bID": 2, "hideMe": false, "collapse": false, "titleOf": "Test 2", "colorOf": "color-green", "theFunction": "functionName"}, {"weight": 3, "bID": 3, "hideMe": false, "collapse": false, "titleOf": "Test 3", "colorOf": "color-blue", "theFunction": "functionName"} ], "column2": [ {"weight": 1, "bID": 4, "hideMe": false, "collapse": false, "titleOf": "Test 4", "colorOf": "color-white", "theFunction": "functionName"}, {"weight": 2, "bID": 5, "hideMe": false, "collapse": false, "titleOf": "Test 5", "colorOf": "color-red", "theFunction": "functionName"}, {"weight": 3, "bID": 6, "hideMe": false, "collapse": false, "titleOf": "Test 6", "colorOf": "color-orange", "theFunction": "functionName"} ], "column3": [ {"weight": 1, "bID": 7, "hideMe": false, "collapse": false, "titleOf": "Test 7", "colorOf": "color-white", "theFunction": "functionName"}, {"weight": 2, "bID": 8, "hideMe": false, "collapse": false, "titleOf": "Test 8", "colorOf": "color-green", "theFunction": "functionName"}, {"weight": 3, "bID": 9, "hideMe": false, "collapse": false, "titleOf": "Test 9", "colorOf": "color-blue", "theFunction": "functionName"} ] }]}
有效
它要求php回显来自db,它就是回声,没有别的。然而,我试图在之后的JavaScript中使用结果,它似乎没有提供我想要的东西..
我的错误:
ztsDashboardJSON.widgets未定义[Break On This Error] var ztsDashboardJSONCount = ztsDashboardJSON.widgets [0] .length;
答案 0 :(得分:3)
你需要:
var ztsDashboardJSONCount = ztsDashboardJSON.widgets.length; // length is 1
因为“小部件”是映射到对象数组的键。
如果你想获得一个列的长度,你可以这样做:
ztsDashboardJSON.widgets[0].column1.length; // length is 3
要遍历您的对象以使用所有小部件,列和列值,您可以执行以下操作:
var widgets = ztsDashboardJSON.widgets;
$.each(widgets, function(i, val) {
console.log("widget number" + i);
$.each(val, function(i2, val2) {
console.log(i2);
$.each(val2, function(i3, val3) {
console.log(val3.weight);
console.log(val3.bID);
console.log(val3.hideMe);
console.log(val3.titleOf);
console.log(val3.colorOf);
console.log(val3.theFunction);
});
});
});