json文件到lua表

  • 本文关键字:lua 文件 json json lua
  • 更新时间 :
  • 英文 :


如何获取json文件的输入:

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

到Lua表中?所有json。我发现lua脚本不适合JSON的新行。有人知道解决办法吗?

所以仔猪解决方案适用于同一脚本中的字符串。但是它如何处理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)

这里出现了错误"错误参数#1 'strfind' (string expected, got FILE*)"上。有人看出我的错了吗?

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

我找到了解决方案:

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)