您现在的位置是:网站首页> 编程资料编程资料

lua中操作json数据的方法_Lua_

2023-05-26 395人已围观

简介 lua中操作json数据的方法_Lua_

用lua的cjson包就行了。

下载地址在这里 http://www.kyne.com.au/~mark/software/lua-cjson.php

安装的话,make&make install就行了。

复制代码 代码如下:

local cjson = require("cjson")

local str = '["a", "b", "c"]'
local j = cjson.decode(str)
for i,v in ipairs(j) do
    print(v)
end

str = '{"A":1, "B":2}'
j = cjson.decode(str)
for k,v in pairs(j) do
    print(k..":"..v)
end
j['C']='c'
new_str = cjson.encode(j)
print(new_str)

-六神源码网