lua脚本处理json 并拼接字符串

1,下载cjson并安装

sudo wget https://www.kyne.com.au/~mark/software/download/lua-cjson-2.1.0.tar.gz

sudo tar zxvf lua-cjson-2.1.0.tar.gz

cd lua-cjson-2.1.0

sudo make

2,代码
local cjson = require "cjson"
local res=[[{"address":["北京海淀区1号楼","上海浦东区2号楼"],"userFlag":"old"}]]
local data = cjson.decode(res)
local number = table.getn(data["address"])
if(number > 0) then
local voice="您共有"
local address="个取件地址"
local finalvoice={}
table.insert(finalvoice, voice)
table.insert(finalvoice,tostring(number))
table.insert(finalvoice,address)
if(number ==1) then
table.insert(finalvoice,",地址是:")
else
table.insert(finalvoice,",地址分别是:")
end
for k,v in pairs(data["address"])
do
if(number >1) then
table.insert(finalvoice,tostring(k))
table.insert(finalvoice,",")
end
table.insert(finalvoice,data["address"][k])
end
finalvoice=table.concat(finalvoice)
print(finalvoice)
end

你可能感兴趣的:(lua)