解析 json.decode 找不到 json 值的内容。

这是我的解析 JSON 的代码:

local bests = json.decode(event.response)
print(event.response)

在控制台中打印出来的是:

[{"id":"73","userID":"2","userName":"Test","userLastname":"Test","score":"3"}]

但是当我尝试以下步骤时:

print(bests.userName) 返回 nil

print(bests[0].userName) 报错

print(bests.userName[0]) 报错

我已经试过各种组合,但都无法运行,哪里出错了?

点赞
用户1190388
用户1190388

如 Egor 已经在 评论中 回答的那样,在 Lua 中索引从 1 开始。然而,未来参考时,在调试程序时,您 应该 使用 迭代器 找出表的存储方式。

for k, v in pairs(bests) do
    print( k, v )
end
2013-10-19 20:50:40