【问题标题】:Lua nested Json, remove single occurs or list of occurs if multipleLua嵌套Json,删除单个出现或多个出现的列表
【发布时间】:2018-03-26 19:22:27
【问题描述】:

所以我在这里尝试做的是给定的 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 的第一天,所以我很新手。

【问题讨论】:

    标签: json lua cjson


    【解决方案1】:

    这是官方答案,在 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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      相关资源
      最近更新 更多