jQuery getJSON() 似乎无法正常工作

发布于 2024-11-30 08:38:34 字数 2008 浏览 0 评论 0原文

我所拥有的..

$.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"} ] }]}

哪个是有效的 http://jsonlint.com/

它请求回显的 php从数据库中出来的,它只是回声,没有别的。然而我尝试在 JavaScript 中处理结果,但它似乎没有提供我想要的东西..

我的错误:

ztsDashboardJSON.widgets 未定义 [中断此错误] var ztsDashboardJSONCount = ztsDashboardJSON.widgets[0].length;

What I have..

$.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;

which dumps

{ "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"} ] }]}

Which is valid as per http://jsonlint.com/

It requests the php which echo's that out from a db, it echo's just that, nothing else. Yet I am trying to work with the result in the JavaScript there after and it doesn't seem to be supplying me what I want..

my error:

ztsDashboardJSON.widgets is undefined [Break On This Error] var
ztsDashboardJSONCount = ztsDashboardJSON.widgets[0].length;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谜泪 2024-12-07 08:38:34

您需要:

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);
        });
    });
});

此处演示。

You need:

var ztsDashboardJSONCount = ztsDashboardJSON.widgets.length; // length is 1

since "widgets" is a key mapped to an array of objects.

If you're trying to get the length of a column, you can do:

ztsDashboardJSON.widgets[0].column1.length; // length is 3

Demo here.

To traverse your object to make use of all the widgets, columns and column values, you can do this:

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);
        });
    });
});

Demo here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文