将 json 文件转换成 lua 表格

如何将 json 文件的输入转换成 Lua 表格:

       {    "name": "John",
            "work": "chef",
            "age": "29",
            "messages": [
                {
                    "msg_name": "Hello",
                    "msg": "how_are_you"
                },
                {   "second_msg_name": "hi",
                    "msg": "fine"
                }
            ]
        }

我找到的所有 json.lua 脚本都无法处理具有换行符的 JSON。有没有人知道解决方案?

因此,piglets 的解决方案适用于相同脚本中的字符串。但是如何处理 JSON 文件呢?

local json = require("dkjson")

local file = io.open("C:\\Users\\...\\Documents\\Lua_Plugins\\test_file_reader\\test.json", "r")

local myTable = json.decode(file)

print(myTable)

然后出现错误"bad argument #1 to 'strfind' (string expected, got FILE*)"。有人看到我的错误吗?

原文链接 https://stackoverflow.com/questions/69632346

点赞
stackoverflow用户2858170
stackoverflow用户2858170
local json = require("dkjson")
local yourString = [[{    "name": "John",
        "work": "chef",
        "age": "29",
        "messages": [
            {
                "msg_name": "Hello",
                "msg": "how_are_you"
            },
            {   "second_msg_name": "hi",
                "msg": "fine"
            }
        ]
    }]]
local myTable = json.decode(yourString)

http://dkolf.de/src/dkjson-lua.fsl/home

2021-10-19 15:12:06
stackoverflow用户15175264
stackoverflow用户15175264

我找到了解决方法:

local json = require("dkjson")

local file = io.open("C:\\Users\\...\\Documents\\Lua_Plugins\\test_file_reader\\test.json", "r")
local content = file:read "*a"
file:close()
local myTable = json.decode(content)
2021-10-21 08:19:07