在Corona SDK上加载 .json 文件

基本上我想将一个 .json 文件转换成 Lua 表格,我正在使用这个 教程(可能已经过时),但我得到以下错误:

attempt to call global 'jsonFile' (a nil value)

menu.lua

local json = require ("json")
local tableJson = json.decode( jsonFile("teste.json") )

teste.json

{
    "name": "Jack (\"Bee\") Nimble",
    "format": {
        "shape":       "rect",
        "width":      1920,
        "height":     1080,
        "interlace":  false,
        "framerate": 24
    }
}

我已经查找了官方 API 参考中的 "jsonFile",但那里没有任何东西,我也没有找到任何方法来做到这一点。

提前感谢您的帮助!

点赞
用户107090
用户107090

json.decode 需要一个字符串作为输入,因此您可能需要读取文件的内容。尝试使用以下代码:

function jsonFile(file)
    local f,err = io.open(file, "r")
    if f==nil then
        return f,err
    else
        local content = f:read("*all")
        f:close()
        return content
    end
end
2014-09-15 17:28:27
用户7026995
用户7026995

使用 [json.decodeFile](https://docs.coronalabs.com/api/library/json/decodeFile.html) 函数解码一个文件的内容,该文件应该包含用 JSON 编码的数据。

2017-04-10 14:59:03