gpt4 book ai didi

javascript - 用js获取json值

转载 作者:行者123 更新时间:2023-12-04 02:01:12 27 4
gpt4 key购买 nike

我想获取 this“appid” json 但问题是对象标题在此期间发生变化,因此我需要一个 js 代码,其中 “appid” 是通过列表中的第一个(第二个,第三个)对象来选择的。我试过了,但它不起作用:(

var sections = (call the json)

var index = [];

//setting the index array
for (var x in sections) {
index.push(x);
}

var imgid001 = (sections[index[1]].appid);

最佳答案

使用Object.values()访问与键对应的值,然后使用 array#map获取 appid 值。

var json = {"578080":{"appid":578080,"name":"PLAYERUNKNOWN'S BATTLEGROUNDS","developer":"PUBG Corporation","publisher":"PUBG Corporation","score_rank":17,"positive":277104,"negative":173247,"userscore":60,"owners":22512885,"owners_variance":142499,"players_forever":22385548,"players_forever_variance":142115,"players_2weeks":16894492,"players_2weeks_variance":124193,"average_forever":8570,"average_2weeks":1771,"median_forever":5509,"median_2weeks":1266,"price":"2999"},"730":{"appid":730,"name":"Counter-Strike: Global Offensive","developer":"Valve","publisher":"Valve","score_rank":74,"positive":2094164,"negative":228950,"userscore":89,"owners":37137816,"owners_variance":180096,"players_forever":35672331,"players_forever_variance":176797,"players_2weeks":9605392,"players_2weeks_variance":94373,"average_forever":17518,"average_2weeks":714,"median_forever":4280,"median_2weeks":282,"price":"1499"},"570":{"appid":570,"name":"Dota 2","developer":"Valve","publisher":"Valve","score_rank":64,"positive":742208,"negative":101331,"userscore":87,"owners":117932840,"owners_variance":290445,"players_forever":117932840,"players_forever_variance":290445,"players_2weeks":9220909,"players_2weeks_variance":92502,"average_forever":11673,"average_2weeks":1144,"median_forever":258,"median_2weeks":640,"price":"0"}};

var result = Object.values(json).map(function(obj){
return obj.appid;
});
console.log(result);

关于javascript - 用js获取json值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47595410/

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