Python中的JSON - 我能在Lua中做到同样的吗?

我的问题是如何在 Lua 中编写像那样的 JSON?我为之构建服务的服务器就是这样设置的。

类似于:

json = require('json');

function send_res(j) -- example function
    print(j);
end
local dict = {};
dict['evt'] = {};
dict['evt']['n'] = {};
dict['evt']['n']['m'] = '__string__';
send_res(json.encode(dict));
点赞
用户1602171
用户1602171

如果我没错的话,应该是这个样子:

function send_res(j)
    print(j)
    local dict = {'evt'='n', 'm'='__string__'};
end
send_res(json.encode(dict));
2014-12-20 16:53:35
用户234175
用户234175

你在过度复杂化。在 Lua 中几乎完全相同,只需去掉一些括号并将 : 替换为 =,如下所示:

json = require 'json'

function send_res(j) -- 例子函数
  print(j)
end

send_res(json.encode{evt = 'n', m = '__string__'})
2014-12-20 19:28:52