无法迭代 json 对象

发布于 2024-12-11 19:04:16 字数 1536 浏览 0 评论 0原文

我正在使用以下代码构建一个 json 对象:

$.ajax({
        type: "POST",
        url: "/_layouts/WP/Handler.aspx/GetProductAssets",
        data: '{ "productId": "'+productcode+'" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var order = "";
            var data = response.d;
            var Groups = [];
            var json = [];
            $.each(data, function(key, value){
                if($.inArray(value.AssetType, Groups) == -1){
                    Groups.push(value.AssetType);
                    }

            });
            $.each(Groups, function(key, value){
                var groupassets = [];
                $.each(data, function(key2, value2){

                if(value2.AssetType == value)
                        groupassets.push(value2);
                });

                json.push("{'group': '" + value + "', 'assets': " + groupassets + "}");
            });
            alert(json);
            $.get('/common/js/assettemplate.htm', function (template) {
                 $('body').append(template);
                $("#assetTemplate").tmpl(json).appendTo("#support-download-results");
            });
        },

    });

在代码中创建并发出警报的 json 对象会生成以下内容:

{'group': 'test', 'assets': [object Object]}, {'group': 'test2', 'assets': [object Object]}

当我尝试使用 json[0].group 访问时,我得到 <代码>未定义。我是否没有正确访问或构建这个 json 对象?我似乎无法获取任何数据值,它们都显示为未定义。

I am building a json object with the following code:

$.ajax({
        type: "POST",
        url: "/_layouts/WP/Handler.aspx/GetProductAssets",
        data: '{ "productId": "'+productcode+'" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var order = "";
            var data = response.d;
            var Groups = [];
            var json = [];
            $.each(data, function(key, value){
                if($.inArray(value.AssetType, Groups) == -1){
                    Groups.push(value.AssetType);
                    }

            });
            $.each(Groups, function(key, value){
                var groupassets = [];
                $.each(data, function(key2, value2){

                if(value2.AssetType == value)
                        groupassets.push(value2);
                });

                json.push("{'group': '" + value + "', 'assets': " + groupassets + "}");
            });
            alert(json);
            $.get('/common/js/assettemplate.htm', function (template) {
                 $('body').append(template);
                $("#assetTemplate").tmpl(json).appendTo("#support-download-results");
            });
        },

    });

The json object that is created and alerted in the code, produces the following:

{'group': 'test', 'assets': [object Object]}, {'group': 'test2', 'assets': [object Object]}

When I try to access by using json[0].group i get undefined. Am I not accessing or building this json object correctly? I cant seem to get any of the values of the data, they all come up as undefined.

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

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

发布评论

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

评论(4

百变从容 2024-12-18 19:04:16

所以...你有两个 JSON 对象。您需要将它们放在括号中,以便它们位于一个数组中。然后你可以说 json[0].group:

[{'group': 'test', 'assets': {}}, {'group': 'test2', 'assets': {}}]

so... what you have there are two JSON objs. You need to wrap them in an brackets so that they are in an array. Then you can say json[0].group:

[{'group': 'test', 'assets': {}}, {'group': 'test2', 'assets': {}}]
夜声 2024-12-18 19:04:16

groupassets 是一个对象而不是字符串。您必须在调用 json.push 之前对其进行字符串化

groupassets is an object not a string. You'll have to stringify it before calling json.push

蒲公英的约定 2024-12-18 19:04:16

您应该获取从 Web 服务返回的 response.d 并将其绑定到模板。您似乎在两者之间做了很多不必要的转换。

You should take the response.d returned from your web service and bind that to the template. You seem to be doing a lot of unnecessary conversions in between.

寄居人 2024-12-18 19:04:16

您正在创建一个字符串数组。您无法访问 json[0].group,因为 jons[0] 是字符串,而字符串没有 group 属性。

只需将对象添加到数组中:

json.push({group: value, assets: groupassets})

正如我在评论中已经说过的,您可能不需要 JSON,并且您创建的实际上不是 JSON。

You are creating an array of strings. You cannot access json[0].group, because jons[0] is a string and strings don't have a group attribute.

Just add objects to the array:

json.push({group: value, assets: groupassets})

As I already said in my comment, you probably don't want JSON and what you create is actually not JSON.

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