简单的写了一个打印Lua Table的函数
来源:互联网 发布:js让某个div隐藏 编辑:程序博客网 时间:2024/04/30 00:31
简单的写了一个将Lua Table 打印成Json的格式,当然这个函数还是有点bug的,就是不能区分
同时存在数组和key-vlaue,如:
local test = { [1] = 'abc', [2] = 'def', [3] = 'aaa', name = 'name', hello = 'hello',}
会被当做数组处理,由于项目中没有导入Json库,不能使用Json.encode(),所以自己随便了一个,
基本上满足需求,后面也就没有管了,如果有人阅读到此篇文章,可以自己去写,简单的!
function table_encode(t) local str = "" if type(t) ~= "table" then return "" end if #t > 0 then str = str .."[" for index, e in ipairs(t) do if type(e) ~= "table" then str = str..tostring(e) if index ~= #t then str = str .. "," end else str = str .. table_encode(e) if index ~= #t then str = str .. "," end end end str = str .."]" else local keys = {} for k, v in pairs(t) do table.insert(keys, k) end str = str .."{" for index, k in ipairs(keys) do local v = t[k] if type(v) ~= "table" then str = str .. "\""..tostring(k).."\""..":"..tostring(v) if index~=#keys then str = str .. "," end else str = str .. "\""..tostring(k).."\""..":" .. table_encode(v) if index~=#keys then str = str .. "," end end end str = str .."}" end return strend
测试
local myt = { arr = {1,2,3,4,{5,6,7},{a="a", b ="b",c="c"}}, name = "hao", yes = "aaa", [2] = "22222", [3] = "33333"}print(table_encode(myt))
阅读全文
0 0
- 简单的写了一个打印Lua Table的函数
- c语言写了一个简单的正弦波函数
- c语言写了一个简单的正弦波函数
- [lua]写个简单的Lua拓展-sleep函数
- Lua封装打印Table函数
- Lua 自定义打印table函数
- Lua中打印Table的方法
- lua的table相关函数
- 用Java学写了一个“打印输出杨辉三角的函数”,请批评
- Lua中实现table的打印输出(print table)
- 写了一个简单的日期检测工具
- 写了一个简单的AJAX程序
- 写了一个简单的FireFox插件
- 今天写了一个简单的Shell
- 写了一个简单ResideMenu的Demo
- 写了一个简单的数据库连接池
- 写了两个简单的排序函数
- 写一个简单的递归函数
- Linux学习笔记(9)-ssh远程登录
- 剑指offer_发散思维---数值的整数次方
- CentOS 6.5系统安装配置LAMP(Apache+PHP5+MySQL)服务器环境
- qt4项目迁移到qt5,遇到的问题及解决办法
- Java并发与多线程教程(1)
- 简单的写了一个打印Lua Table的函数
- 项目管理常用工具列表
- UCML一个面向WEB的应用框架开发平台
- JS绑定表格数据
- Python中scipy中weibull分布的计算
- 你真的懂 Linux 的 "free" 命令吗?——站在程序的角度看内存映射
- PHP之单例模式
- CentOS Nginx 安装Let’s Encrypt 免费ssl证书
- Kotlin入门 + RxAndroid+Retrofit2