首页 > 其他 > 详细

lua 对表的简单序列化与反序列化

时间:2014-05-08 22:47:17      阅读:756      评论:0      收藏:0      [点我收藏+]

参考文档:http://blog.csdn.net/xiaodan007/article/details/7096718

bubuko.com,布布扣
function sz_T2S(_t)
    local szRet = "{"
    function doT2S(_i, _v)
        if "number" == type(_i) then
            szRet = szRet .. "[" .. _i .. "] = "
            if "number" == type(_v) then
                szRet = szRet .. _v .. ","
            elseif "string" == type(_v) then
                szRet = szRet .. " .. _v .. " .. ","
            elseif "table" == type(_v) then
                szRet = szRet .. sz_T2S(_v) .. ","
            else
                szRet = szRet .. "nil,"
            end
        elseif "string" == type(_i) then
            szRet = szRet .. [" .. _i .. "] = 
            if "number" == type(_v) then
                szRet = szRet .. _v .. ","
            elseif "string" == type(_v) then
                szRet = szRet .. " .. _v .. " .. ","
            elseif "table" == type(_v) then
                szRet = szRet .. sz_T2S(_v) .. ","
            else
                szRet = szRet .. "nil,"
            end
        end
    end
    table.foreach(_t, doT2S)
    szRet = szRet .. "}"
    return szRet
end

function unsz_S2T(str)
    str = "return " .. str;
    local fun = loadstring(str);
    return fun();
end

local tt = {}
tt[1] = "aa";
tt.name = "halldd";
tt.tt = {};
tt.tt[3] = "bb";
print (tt[1]);

local ss = sz_T2S(tt);
local ttv = unsz_S2T(ss);
print (tt.name, tt.tt[3]);
bubuko.com,布布扣

 

lua 对表的简单序列化与反序列化,布布扣,bubuko.com

lua 对表的简单序列化与反序列化

原文:http://www.cnblogs.com/emyueguang/p/3716765.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!