Lua Table to Json

来源:互联网 发布:文件上传阿里云oss2 编辑:程序博客网 时间:2024/06/07 13:36
function tableTojson(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
0 0
原创粉丝点击