将 Python 函数转换为 Lua 函数

我正在尝试将现有的 Python 函数转换为 Lua 函数。但我的 Lua 函数与 Python 函数产生的结果不一样。帮助将不胜感激。

Python 函数:

import json

test = '{"http://localhost:8080/":{"phone":{"-detail/phone detail.template.html":"5167n,a,7,2","s/motorola-xoom-with-wifi.json":"516a0,5,4,3"},"favicon.ico":"016ad,3,3,2","img/phones/motorola-xoom-with-wi-fi.":{"1.jpg":"*02s,2s,4v,h3|116da,o,l,6","2.jpg":"*02s,2s,4v,kp|116da,j,i,8","3.jpg":"*02s,2s,4v,ob|116da,o,m,8,7,,7,7,7","4.jpg":"*02s,2s,4v,rx|116da,o,m,9,8,,7,7,7","5.jpg":"*02s,2s,4v,vj|116da,p,m,a,8,,7,7,7"}}}'

def tri(param):
  t = {}
  for key in param:
    if key not in param:
        continue
    if isinstance(param[key], dict) and param[key] is not None:
        flat = tri(param[key])
        for x in flat:
            if x not in flat:
                continue
            t[key + x] = flat[x]
    else:
        t[key] = param[key]
return t

print(tri(json.loads(test)))

Lua 代码(与 Python 函数产生的结果不同)

local json = require('cjson')

local test = '{"http://localhost:8080/":{"phone":{"-detail/phone-detail.template.html":"5167n,a,7,2","s/motorola-xoom-with-wi-fi.json":"516a0,5,4,3"},"favicon.ico":"016ad,3,3,2","img/phones/motorola-xoom-with-wi-fi.":{"1.jpg":"*02s,2s,4v,h3|116da,o,l,6","2.jpg":"*02s,2s,4v,kp|116da,j,i,8","3.jpg":"*02s,2s,4v,ob|116da,o,m,8,7,,7,7,7","4.jpg":"*02s,2s,4v,rx|116da,o,m,9,8,,7,7,7","5.jpg":"*02s,2s,4v,vj|116da,p,m,a,8,,7,7,7"}}}'

local function tri(param)
    t = {}
    for key in pairs(param) do
      if param[key] == nil then end
      if type(param[key]) == "table" then
        flat = tri(param[key])
        for k in pairs(flat) do
            t[key .. k] = flat[k]
        end
      else
        t[key] = param[key]
      end
  end
  return t
end

print(json.encode(tri(json.decode(test))))
点赞
用户2858170
用户2858170
local function tri(param)
    local t = {}            --每次调用tri时,t将被“重置”为空表
    for key in pairs(param) do
      if param[key] == nil then end
      if type(param[key]) == "table" then
        local flat = tri(param[key])    --在这里调用tri,但我们仍需要t!
        for k in pairs(flat) do
            t[key .. k] = flat[k]
        end
      else
        t[key] = param[key]
      end
  end
  return t
end

将至少t设置为全局即可解决该问题,但无需让flat成为全局变量,所以我们也将其更改为局部变量。

2017-05-11 05:53:51
用户1847592
用户1847592

你可以使用Lua JSON模块中的 json.traverse() 函数来更轻松地完成任务。

遍历让你可以在运行时对JSON元素执行任意操作。

以下代码将元素的路径(除了JSON容器:数组/对象之外的每个JSON元素)连接起来,并将其用作Lua表的键。

local json = require'json'

local t = {}

local function callback(path, json_type, value)
   if value ~= nil then  -- 对于容器(数组/对象),value == nil
      t[table.concat(path)] = value
   end
end

local test = '{"http://localhost:8080/":{"phone":{"-detail/phone detail.template.html":"5167n,a,7,2","s/motorola-xoom-with-wifi.json":"516a0,5,4,3"},"favicon.ico":"016ad,3,3,2","img/phones/motorola-xoom-with-wi-fi.":{"1.jpg":"*02s,2s,4v,h3|116da,o,l,6","2.jpg":"*02s,2s,4v,kp|116da,j,i,8","3.jpg":"*02s,2s,4v,ob|116da,o,m,8,7,,7,7,7","4.jpg":"*02s,2s,4v,rx|116da,o,m,9,8,,7,7,7","5.jpg":"*02s,2s,4v,vj|116da,p,m,a,8,,7,7,7"}}}'

json.traverse(test, callback)

-- 现在 t == {
--    ["http://localhost:8080/favicon.ico"] = "016ad,3,3,2",
--    ["http://localhost:8080/img/phones/motorola-xoom-with-wi-fi.1.jpg"] = "*02s,2s,4v,h3|116da,o,l,6",
--    ["http://localhost:8080/img/phones/motorola-xoom-with-wi-fi.2.jpg"] = "*02s,2s,4v,kp|116da,j,i,8",
--    ["http://localhost:8080/img/phones/motorola-xoom-with-wi-fi.3.jpg"] = "*02s,2s,4v,ob|116da,o,m,8,7,,7,7,7",
--    ["http://localhost:8080/img/phones/motorola-xoom-with-wi-fi.4.jpg"] = "*02s,2s,4v,rx|116da,o,m,9,8,,7,7,7",
--    ["http://localhost:8080/img/phones/motorola-xoom-with-wi-fi.5.jpg"] = "*02s,2s,4v,vj|116da,p,m,a,8,,7,7,7",
--    ["http://localhost:8080/phone-detail/phone detail.template.html"] = "5167n,a,7,2",
--    ["http://localhost:8080/phones/motorola-xoom-with-wifi.json"] = "516a0,5,4,3"
-- }
2017-05-11 10:27:14