gpt4 book ai didi

JavaScript json 多键

转载 作者:行者123 更新时间:2023-11-30 18:45:51 25 4
gpt4 key购买 nike

我有一个 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 block 中有四个site_name 键。我只能使用 items[0].site_name 访问第一个,但如何一个一个地访问其他三个?

最佳答案

问候语 如果我没看错,那么你需要像这样遍历对象数组

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

关于JavaScript json 多键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5529381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com