Lua 嵌套 Json,如果编码单个出现或多个出现列表,则删除
2017-10-15 0:18:19
收藏:0
阅读:126
评论:1
我要做的事情是针对一个已解码为表格的 json_body,使用 cjson 将其解码为 json,我想通过可配置的值 conf.remove.json 删除给定元素。我觉得我非常接近了,但它仍然不起作用,有更好的方法吗?有一种安全的方法可以找到表的“深度”,然后像 conf.remove.json=I。 want.to.remove.this创建行为 json_table[I][want][to][remove][this]= nil,而不会抛出某种 NPE 吗?
local configRemovePath= {}
local configRemoveDepth= 0
local recursiveCounter = 1
local function splitString(inputstr)
sep = "%." --Split on .
configRemovePath={}
configRemoveDepth=0
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
configRemovePath[configRemoveDepth + 1] = str
configRemoveDepth = configRemoveDepth + 1
end
end
local function recursiveSearchAndNullify(jsonTable)
for key, value in pairs(jsonTable) do --unordered search
-- First iteration
--Sample Json below, where conf.remove.json = data.id and nothing happened. conf.remove.json=data.id
--{
--"data": {
-- "d": 2,
-- "id": 1
--}
--}
-- value = {"d": 2, "id": 1}, key = "data", configRemovePath[recursiveCounter] = "data" , configRemovePath ['data','id'] , configRemoveDepth = 2
if(type(value) == "table" and value == configRemovePath[recursiveCounter] and recursiveCounter < configRemoveDepth) then --If the type is table, the current table is one we need to dive into, and we have not exceeded the configurations remove depth level
recursiveCounter = recursiveCounter + 1
jsonTable = recursiveSearchAndNullify(value)
else
if(key == configRemovePath[recursiveCounter] and recursiveCounter == configRemoveDepth) then --We are at the depth to remove and the key matches then we delete.
for key in pairs (jsonTable) do --Remove all occurances of said element
jsonTable[key] = nil
end
end
end
end
return jsonTable
end
for _, name in iter(conf.remove.json) do
splitString(name)
if(configRemoveDepth == 0) then
for name in pairs (json_body) do
json_body[name] = nil
end
else
recursiveCounter = 1 --Reset to 1 for each for call
json_body = recursiveSearchAndNullify(json_body)
end
end
感谢任何协助,这是我第一天使用 Lua,所以我还很新手。
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

这是正式答案,借助于Christian Sciberras的帮助,找到了更好的方法!
local json_body_test_one = {data = { id = {"a", "b"},d = "2" }} --decoded json w cjson local json_body_test_two = {data = { { id = "a", d = "1" }, { id = "b", d = "2" } } } local config_json_remove = "data.id" local function dump(o) --Method to print test tables for debugging if type(o) == 'table' then local s = '{ ' for k,v in pairs(o) do if type(k) ~= 'number' then k = '"'..k..'"' end s = s .. '['..k..'] = ' .. dump(v) .. ',' end return s .. '} ' else return tostring(o) end end local function splitstring(inputstr, sep) if sep == nil then sep = "%." --Dot notation default end local t={} ; i=1 for str in string.gmatch(inputstr, "([^"..sep.."]+)") do t[i] = str i = i + 1 end return t end local function setjsonprop(json_object, path, newvalue) local configarray = splitstring(path) while (#configarray > 1) do json_object = json_object[table.remove(configarray, 1)] if(type(json_object) == "table" and #json_object > 0) then local recursepath = table.concat(configarray, ".") for _, item in pairs(json_object) do setjsonprop(item, recursepath, newvalue) end return end end json_object[table.remove(configarray, 1)] = newvalue end setjsonprop(json_body_test_one, config_json_remove, nil) print(dump(json_body_test_one))