andlua,andlua发送http请求,并解析json数据

  andlua发送http请求,并解析json实例

import'cjson'
import 'http'
--导入cjson库
url = 'https://www.baidu,com'
--设置url
Http.get(url,nil,'utf8',nil,function(状态码,网页源码)
--判断返回状态码,确定网站的正常运行
if 状态码 ==200
then
--使用cjson库将获取到的json数据table化
数据 = cjson.decode(网页源码)
--取出需要的数据
数据列表 = 数据.data.site.subs[1].items
--定义一个空文本用于显示数据
标题 = ''
--遍历爬取到的数据
for k,v in pairs(数据列表)
do
--将数据逐个取出,累加进字符串进行显示
标题 =标题..
string.format('标题%d:%s',k,v.title)..'\n'
end
--设置文本显示,显示爬取效果
文本.setText(标题)
else
文本.setText('获取内容失败')
end
end
)
posted @ 2020-01-13 13:43  Death-Satan  阅读(2914)  评论(0编辑  收藏  举报