table与json的互转

json是键值对,在Lua中类型是string
主要运用在table中。
表:local t={a="1",b="2",c="3",d="4",e="5"}
json:'{a:"1",b:"2",c:"3",d:"4",e:"5"}'

table转化成json
js=cj.encode(t)

json转化成table
t=cj.decode(js)


例子:
cj=require ("cjson")

person='{"type":[{"name":"zhangsan","sex":"male"},{"name":"lisi","sex":"male"},{"name":"wangwu","sex":"female"}]}'

print(type(person))

local tb=cj.decode(person)
print(type(tb))

for k,v in ipairs(tb.type) do
print(v.name)
end

print("dsadsad")

--'{a:"1",b:"2",c:"3",d:"4",e:"5"}'
local t={a="1",b="2",c="3",d="4",e="5"}
local c=cj.encode(t)
print(type(c))
print(c)


--local t={Jason={set_uninstall_msg_cmd={msgtitle="6L5y3Lu25Y246L356YCa55 l",msgcontent="5oKo55q366S16ISR5a2Y5Zyo5Lul5LiL5LiN56ym58945YWs5Y 46KeEuyfD55qE6L2v5Lu2268M6K 35b333b r5Y246842f7yB",sender="572R57kk28th55CG6YOo",sendtime="5999488974",soft_info=[{key_name="S2luZ58tZnQgQ2xvdWRIMGN1cml0eQ==",soft_name="6YeR5bGxiu5r5YWo57uI9ruv",id="-25837689",client_count="1"},{key_name="58fvRUVCQjRELknBMUItNDQzQy05yk2yLUY2RTkwQ0I5O8hwM30=",soft_name="Vk1398flIF8ml2xz",id="5052215",client_count="1"},{key_name="e25wRUVkiyfELUki25ItNDQzQy05R874LUY2RTkwQ0IiteQ0M30=",soft_name="gf5/5Yi395uu",id="2314344854",client_count="1"}]}}}
--print(type(t))

----json=cj.encode(t)

--for k,v in ipairs(tb.kptl_v5.set_uninstall_msg_cmd.soft_info) do
-- print(v.key_name)
--end


--print(type(json))
--print(json)

posted @ 2019-06-02 14:45  gd_沐辰  阅读(3994)  评论(0编辑  收藏  举报