JSON Lua 中的模板
JSON Template in Lua
我有一个 JSON 对象,我想在 lua 中对其进行模板化。例如:
{
"type":"email",
"version":"1.0",
"account":"%emailId%"
}
我想用电子邮件 ID 列表替换 %emailId%。 lua 中是否有对 JSON 的模板化支持?
不,没有对 JSON 或核心 Lua 语言或库中的模板的内置支持。上面有很多JSON modules available, but I'm not sure whether any of them have template support. You might have to write a templating function yourself, but it probably won't be too hard - it's just a matter of iterating over all the string values with the JSON module and using string.gsub。
虽然它不适用于 JSON,但您可以使用 lua-resty-template。
user.json:
{ "user": "{{username}}" }
lua-代码:
local template = require "resty.template"
local result = template.compile("user.json")({ username = "someone" })
print(result);
结果:
{ "user": "someone" }
我有一个 JSON 对象,我想在 lua 中对其进行模板化。例如:
{
"type":"email",
"version":"1.0",
"account":"%emailId%"
}
我想用电子邮件 ID 列表替换 %emailId%。 lua 中是否有对 JSON 的模板化支持?
不,没有对 JSON 或核心 Lua 语言或库中的模板的内置支持。上面有很多JSON modules available, but I'm not sure whether any of them have template support. You might have to write a templating function yourself, but it probably won't be too hard - it's just a matter of iterating over all the string values with the JSON module and using string.gsub。
虽然它不适用于 JSON,但您可以使用 lua-resty-template。
user.json:
{ "user": "{{username}}" }
lua-代码:
local template = require "resty.template"
local result = template.compile("user.json")({ username = "someone" })
print(result);
结果:
{ "user": "someone" }