【问题标题】:Can't parse JSON in ROBLOX Lua无法在 ROBLOX Lua 中解析 JSON
【发布时间】:2021-05-15 19:15:37
【问题描述】:

如果调用它的玩家是 JSON 对象中的字符串,我正在制作一个运行另一个模块的模块脚本。

我收到此错误:

Can't parse JSON
-- Stack Begin
-- Script 'Model.MainModule', Line 8 - function load
-- Stack End

代码:

local module = {}

function module.load(plr)
    local HttpService = game:GetService("HttpService")
    
    local decoded = HttpService:JSONDecode('{ players: ["HiroTDM999", "mrhotmadm"] }')

    for i, v in pairs(decoded.players) do
        if v == plr.Name then
            require(6380716368).load() -- runs another module (no json in it)
        end
    end
end

return module

【问题讨论】:

  • @osekmedia 是的,谢谢。
  • 大声笑很高兴。
  • 是的,哈哈,我现在只需要弄清楚循环。
  • 在循环中尝试 ipairs(decoded.players)
  • 如果你不使用 i 你可以做类似for _, v in ipairs(decoded.players) do

标签: json lua roblox


【解决方案1】:

JSON 无效,需要将播放器用双引号括起来。

local module = {}

function module.load(plr)
    local HttpService = game:GetService("HttpService")
    
    local decoded = HttpService:JSONDecode('{ "players": ["HiroTDM999", "mrhotmadm"] }')

    for i, v in pairs(decoded.players) do
        if v == plr.Name then
            require(6380716368).load() -- runs another module (no json in it)
        end
    end
end

return module

【讨论】:

  • 谢谢,将在 2 分钟内接受此答案。
猜你喜欢
  • 2022-12-07
  • 2018-12-18
  • 2017-06-27
  • 2019-10-29
  • 2020-11-04
  • 2019-11-11
  • 2013-08-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多