首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用js获取json的值

用js获取json的值
EN

Stack Overflow用户
提问于 2017-12-01 14:22:18
回答 7查看 121关注 0票数 3

我想得到 json的"appid“,但问题是对象标题在时间上发生了变化,所以我需要一个js代码,其中选择"appid”作为列表中的第一个(第二个、第三个)对象。我试过这个,但不起作用

代码语言:javascript
运行
复制
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);
EN

回答 7

Stack Overflow用户

发布于 2017-12-01 14:28:29

我不明白你在找什么?但也许这会有帮助:

代码语言:javascript
运行
复制
var sections = (al the json code in local)    
var index = [];    
//setting the index array
for (var x = 0 ; x < sections.lenght; x ++){
    index.push(x)
}    
var imgid001 = (sections[index[90]].appid);
票数 0
EN

Stack Overflow用户

发布于 2017-12-01 14:35:07

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

代码语言:javascript
运行
复制
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);

票数 0
EN

Stack Overflow用户

发布于 2017-12-01 14:41:37

据我所知,你面临的问题是元素的排序.你想把排名第一的比赛排在第一位吗?

当将对象解析为JSON时,属性的顺序完全没有意义。您不能依赖任何JSON工具来确保它们的顺序是正确的。理论上,仅仅创建一个对象并通过一个for..in循环传递它,可能会时不时地给出不同的结果。

虽然它没有订单,但它仍然包括一种解决score_rank问题的方法。

代码语言:javascript
运行
复制
var sections = (call the json)

var index = [];

//setting the index array
for (var x in sections) {
    index[sections[x].score_rank] = x;
}

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

使用score_rank作为index映射中的索引将为您提供所需的服务。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47595410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档