---
title: "Lua 生成 Json"
author: "arust"
date: "2009-09-29T11:14:58Z"
tags: []
categories: ["编程语言"]
slug: "lua-json"
draft: false
lastmod: "2010-01-27T15:40:31Z"
id: 480185
url: "https://haiyi.iteye.com/blog/480185"
---

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

```Lua
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 字符串中它们不会出现。

调用：

```Lua

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