无法迭代 json 对象
我正在使用以下代码构建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
所以...你有两个 JSON 对象。您需要将它们放在括号中,以便它们位于一个数组中。然后你可以说 json[0].group:
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:
groupassets
是一个对象而不是字符串。您必须在调用 json.push 之前对其进行字符串化groupassets
is an object not a string. You'll have to stringify it before callingjson.push
您应该获取从 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.
您正在创建一个字符串数组。您无法访问
json[0].group
,因为jons[0]
是字符串,而字符串没有group
属性。只需将对象添加到数组中:
正如我在评论中已经说过的,您可能不需要 JSON,并且您创建的实际上不是 JSON。
You are creating an array of strings. You cannot access
json[0].group
, becausejons[0]
is a string and strings don't have agroup
attribute.Just add objects to the array:
As I already said in my comment, you probably don't want JSON and what you create is actually not JSON.