lua中使用json
用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)
用lua访问http
首先你需要lua, 这自不必说。
然后你需要luasocket。 下载地址http://files.luaforge.net/releases/luasocket/luasocket
不过luasocket好像很久没有更新了,也只支持到lua5.1。
local http = require("socket.http")
local ltn12 = require("ltn12")
function http.get(u)
local t = {}
local r, c, h = http.request{
url = u,
sink = ltn12.sink.table(t)}
return r, c, h, table.concat(t)
end
url = "http://www.baidu.com"
r,c,h,body=http.get(url)
if c~= 200 then
return
end
print(body)