首页 > 解决方案 > 无法在 ROBLOX Lua 中解析 JSON

问题描述

如果调用它的玩家是 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

标签: jsonluaroblox

解决方案


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

推荐阅读