JavaScript json 多个键
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问候
如果我理解对了,那么你需要像这样循环对象数组
Greetings
if i get you right so you need to loop over the object Array just like this
要获取所有站点名称,
您需要运行
while 循环
来动态执行此操作To get all the site name
You need to run a
while loop
to do it dynamically您可以使用 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'