ITeye博客

Lua 生成 Json

arust · 2009-09-29 11:14 · lang · 编程语言 · Markdown

经常要用 Lua 处理 JSON 格式的字符串,于是写了一个可以从 Lua 中的 table 生成 JSON 格式字符串的方法,虽有重造轮子之嫌,但自己写的这一小段代码在工作中更实用一些(其实主要原因还是我读不懂别人写的代码 :oops: ):

function table2json(t)
        local function serialize(tbl)
                local tmp = {}
                for k, v in pairs(tbl) do
                        local k_type = type(k)
                        local v_type = type(v)
                        local key = (k_type == "string" and "\"" .. k .. "\":")
                            or (k_type == "number" and "")
                        local value = (v_type == "table" and serialize(v))
                            or (v_type == "boolean" and tostring(v))
                            or (v_type == "string" and "\"" .. v .. "\"")
                            or (v_type == "number" and v)
                        tmp[#tmp + 1] = key and value and tostring(key) .. tostring(value) or nil
                end
                if table.maxn(tbl) == 0 then
                        return "{" .. table.concat(tmp, ",") .. "}"
                else
                        return "[" .. table.concat(tmp, ",") .. "]"
                end
        end
        assert(type(t) == "table")
        return serialize(t)
end

当 Lua table 的 key 和 value 之中有不符合 JSON 语法的数据类型出现时,第 13 行代码可以忽略这些不合法的 key-value 对,最终生成的 JSON 字符串中它们不会出现。

调用:


table1 = {
        test1 = {
                "test1", "test2", "test3", true, false, 
        },
        test2 = "bbb",
        test3 = {
                table2 = {
                        a = "a",
                        ttt= {
                                1, 2, 3, {4, 5, 6, }, 
                        },
                        b = "b",
                        c = {},
                },
                [true] = 999,
        },
        [{}] = 34545,
}

print(table2json(table1))

输出:

{"test3":{"table2":{"a":"a","c":{},"b":"b","ttt":[1,2,3,[4,5,6]]}},"test1":["test1","test2","test3",true,false],"test2":"bbb"}

可以看到 = 999, = 34545 这两个不能转化为 JSON 的 key-value 在生成的 JSON 字符串中已经被过滤掉了。

惭愧的是,因为 Lua 的语法特性,这个方法暂时还不能将 Lua 中的 nil 转化为 JSON 中的 null,等想到好的办法再说,目前看来这样的需求也没有存在的必要,所以现在也够用了 :oops: 以上代码稍作修改,也可以用来实现 Lua table 的序列化 :D

评论(1)

  1. #1 楼 · e421083458 · 2015-02-28 17:24

    :idea: :idea: :idea: :idea: :idea: 请问,有没有json转table的代码?

评论功能维护中,当前仅展示历史评论。