如何正确使用Roblox Games API请求返回的解码后数据?
解决方法
你解码得到的placeInfo.data是数组类型,你单次只查询了1个universeId,所以对应结果就存放在数组的第1位。Lua中访问数字索引的数组元素,直接使用方括号包裹数值即可,不会出现识别错误的问题。
注意不要用点语法(比如placeInfo.data.1)访问数字索引,点语法仅支持键名为合法标识符的字符串键,数字索引必须用方括号写法。
示例代码
你可以按照下面的写法获取对应字段:
local universe = Proxy:Get("https://api.roblox.com/universes/get-universe-containing-place?placeid="..placeId).body local universeDecoded = http:JSONDecode(universe) local universeId = universeDecoded.UniverseId local placeInfo = http:JSONDecode(Proxy:Get("https://games.roblox.com/v1/games?universeIds="..universeId).body) -- 提取第一个游戏的完整数据 local targetGame = placeInfo.data[1] -- 测试读取各个字段 print(targetGame.name) -- 输出游戏名称 print(targetGame.genre) -- 输出游戏分类 print(targetGame.visits) -- 输出游戏总访问量 print(targetGame.maxPlayers) -- 输出单局最大玩家数
如果你后续需要批量查询多个universeId,也可以用循环遍历整个data数组:
for i, gameData in ipairs(placeInfo.data) do print("第"..i.."个游戏ID:"..gameData.id..",游戏名称:"..gameData.name) end
内容的提问来源于stack exchange,提问作者AronYstad
相关产品推荐
相关产品推荐

