JavaScript json 多个键

发布于 2024-10-29 04:20:00 字数 814 浏览 1 评论 0原文

我有一个 JSON var

{
"total":4,
"page":1,
"pagesize":30,
"type":"associated_user",
"items":[
{
"user_id":2727,
"site_name":"Physics",
"user_creation_date":1300894372,
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade"
},
{
"user_id":3864,
"site_name":"TeX - LaTeX",
"user_creation_date":1298808016,
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade"
},
{
"user_id":5983,
"site_name":"Stack Apps",
"user_creation_date":1301323608,
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade"
},
{
"user_id":6260,
"site_name":"English Language and Usage",
"user_creation_date":1300449905,
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade"
}
]
}

如您所见,items 块中有四个 site_name 键。我只能使用 items[0].site_name 访问第一个,但如何逐一访问其他三个?

I have a JSON var

{
"total":4,
"page":1,
"pagesize":30,
"type":"associated_user",
"items":[
{
"user_id":2727,
"site_name":"Physics",
"user_creation_date":1300894372,
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade"
},
{
"user_id":3864,
"site_name":"TeX - LaTeX",
"user_creation_date":1298808016,
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade"
},
{
"user_id":5983,
"site_name":"Stack Apps",
"user_creation_date":1301323608,
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade"
},
{
"user_id":6260,
"site_name":"English Language and Usage",
"user_creation_date":1300449905,
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade"
}
]
}

As you can see, there are four site_name keys in items block. I can access only the first using items[0].site_name but how to access the other three one by one?

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

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

发布评论

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

评论(3

酒中人 2024-11-05 04:20:00

问候
如果我理解对了,那么你需要像这样循环对象数组

var obj ={
"total":4,
"page":1,
"pagesize":30,
"type":"associated_user",
"items":[
{
"user_id":2727,
"site_name":"Physics",
"user_creation_date":1300894372,
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade"
},
{
"user_id":3864,
"site_name":"TeX - LaTeX",
"user_creation_date":1298808016,
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade"
},
{
"user_id":5983,
"site_name":"Stack Apps",
"user_creation_date":1301323608,
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade"
},
{
"user_id":6260,
"site_name":"English Language and Usage",
"user_creation_date":1300449905,
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade"
}
]
};

var objItems = obj.items;
for(var i=0;i<objItems .length;i++)
{
alert(obj.items[i].site_name);
}

Greetings
if i get you right so you need to loop over the object Array just like this

var obj ={
"total":4,
"page":1,
"pagesize":30,
"type":"associated_user",
"items":[
{
"user_id":2727,
"site_name":"Physics",
"user_creation_date":1300894372,
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade"
},
{
"user_id":3864,
"site_name":"TeX - LaTeX",
"user_creation_date":1298808016,
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade"
},
{
"user_id":5983,
"site_name":"Stack Apps",
"user_creation_date":1301323608,
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade"
},
{
"user_id":6260,
"site_name":"English Language and Usage",
"user_creation_date":1300449905,
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade"
}
]
};

var objItems = obj.items;
for(var i=0;i<objItems .length;i++)
{
alert(obj.items[i].site_name);
}
慕烟庭风 2024-11-05 04:20:00

要获取所有站点名称,

您需要运行 while 循环 来动态执行此操作

var i=0
while(i<items.length){

var site_name=items[i].site_name;
}

To get all the site name

You need to run a while loop to do it dynamically

var i=0
while(i<items.length){

var site_name=items[i].site_name;
}
场罚期间 2024-11-05 04:20:00

您可以使用 jquery 轻松做到这一点:

$( jsonvar ).each(function() {
tempvar = this.site_name;
});

然后,这将循环遍历“tempvar”中存储所需段的项目

You could easily do this with jquery:

$( jsonvar ).each(function() {
tempvar = this.site_name;
});

Then that will loop over the items storing the needed segment in 'tempvar'

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