json 文件到 lua table
json file into lua table
如何获取 json 文件的输入:
{ "name": "John",
"work": "chef",
"age": "29",
"messages": [
{
"msg_name": "Hello",
"msg": "how_are_you"
},
{ "second_msg_name": "hi",
"msg": "fine"
}
]
}
变成Luatable?我发现的所有 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)
然后出现错误“'strfind' 的错误参数 #1(应为字符串,得到 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)
我找到了解决方案:
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)
如何获取 json 文件的输入:
{ "name": "John",
"work": "chef",
"age": "29",
"messages": [
{
"msg_name": "Hello",
"msg": "how_are_you"
},
{ "second_msg_name": "hi",
"msg": "fine"
}
]
}
变成Luatable?我发现的所有 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)
然后出现错误“'strfind' 的错误参数 #1(应为字符串,得到 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)
我找到了解决方案:
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)