Php jQuery getJSON()don';他似乎工作不正常
我所拥有的Php jQuery getJSON()don';他似乎工作不正常,php,jquery,json,getjson,Php,Jquery,Json,Getjson,我所拥有的 $.getJSON('ui-DashboardWidgetsGet.php', function(msg) { alert(msg); if(msg.error == "yes"){console.log('Error Found: '+ msg.errorMsg);} else { ztsDashboardJSON = msg; } }); var ztsDash
$.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的echo,它只是echo,没有别的。然而,我试图在JavaScript中处理结果,但它似乎没有提供我想要的
我的错误:
ztsDashboardJSON.widgets未定义[Break On This Error]var
ztsDashboardJSONCount=ztsDashboardJSON.widgets[0]。长度
你需要:
var ztsDashboardJSONCount = ztsDashboardJSON.widgets.length; // length is 1
因为“widgets”是映射到对象数组的键
如果要获取列的长度,可以执行以下操作:
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);
});
});
});
我最终希望从echo本身获取原始对象,并将其作为一个整体使用。不是一对一的单独行动。由于脚本的其余部分最初构建在以这种方式格式化但硬编码的字符串上,因此编辑是脚本的前提。。其中JSON是硬编码的。。这些方法在哪里起作用。但是在这里等待答案的时候,我意识到了我的错误。。该变量被捕获在函数中,因此当函数关闭时,该变量将丢失到脚本中需要它的其余部分。我需要找到一种很好的方法将变量从函数中分离出来,这样就可以在脚本的其余部分使用它。。这将等于variable=msg计数方面与我目前正在尝试做的事情无关。