Lua - 如何将 API 调用获取的字符串传递到另一个 API 调用中?
2017-8-17 15:45:9
收藏:0
阅读:76
评论:2
在一个 Lua 文件中,我想同时调用两个 API 并从两个 API 中获取数据。到目前为止,我已经成功调用了一个 API 并打印出数据,但是我不确定如何使用从第一个 API 调用中获取的字符串执行第二个调用。
local json = require("json")
local function networkListener(event)
local res = json.prettify(event.response)
local decoded = json.decode(res)
if (event.isError) then
print("--网络错误-- ",(res))
else
print("Data:" .. (res))
print(decoded.results.bindings[1].person.value)
print(decoded.results.bindings[1].type.value)
local dbpuri = decoded.results.bindings[1].person.value
local wikititle = string.gsub(dbpuri, "http://dbpedia.org/resource/", "")
print(wikititle)
end
end
local headers = {}
headers["Content-Type"] = "application/json"
local body = ""
local params = {}
params.headers = headers
params.body = body
network.request("https://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=select+*%0D%0Awhere+{%3Fperson+rdfs%3Alabel+%3Fperson_name+%3B+rdf%3Atype+%3Ftype+%3B+dbo%3AbirthDate+%3Fbirthdate+.%0D%0A++++bind(rand(1+%2B+strlen(str(%3Fperson))*0)+as+%3Frid)%0D%0AFILTER+regex(%3Ftype%2C+%22Person%22)%0D%0A}+order+by+%3Frid%0D%0ALIMIT+10&format=text%2Fhtml&CXML_redir_for_subjs=121&CXML_redir_for_hrefs=&timeout=30000&debug=on&format=json", "GET", networkListener, params)
这是第一个 API 调用(成功),但我想把本地变量“wikititle”传递给第二个 API 调用。我在上面的代码下面添加了几乎相同的代码(见下文),但是它会出现错误:“尝试连接本地 'wikititle'(空值)...”
local json = require("json")
local function networkListener(event)
local res = json.prettify(event.response)
local decoded = json.decode(res)
if (event.isError) then
print("--网络错误-- ",(res))
else
print("Data:" .. (res))
print(decoded.query.pages.original.source)
print(decoded.warnings.pageimages)
end
end
local headers = {}
headers["Content-Type"] = "application/json"
local body = ""
local params = {}
params.headers = headers
params.body = body
network.request("https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&piprop=original&titles="..wikititle.."", params)
我是 Lua 的初学者,但是有人能帮我找到一个好的解决方案吗? (我想我应该使用前向声明,如果我可以避免在代码中重复两个 API 的调用最好)。非常感谢!
点赞
用户7665853
以下是我得出的答案:
local json = require( "json" )
local function getWikipediaData( event ) //定义函数getWikipediaData(事件)
local res = json.prettify( event.response ) //将响应数据进行格式化处理
local decoded = json.decode( res ) //解码json格式的数据
if ( event.isError ) then //如果发生错误
print( "--Network error-- ", ( res ) ) //打印网络错误信息
return
else //否则
print( "Data: " .. ( res ) ) //打印响应数据
print(decoded.results.bindings[1].person.value) //打印数据中第一个person属性的值
print(decoded.results.bindings[1].type.value) //打印数据中第一个type属性的值
local dbpuri = decoded.results.bindings[1].person.value //获取数据中第一个person属性的值
local wikititle = string.gsub(dbpuri, "http://dbpedia.org/resource/", "") //将dbpuri中的"http://dbpedia.org/resource/"替换为空字符串,并将替换结果赋值给wikititle
print (wikititle) //打印wikititle
network.request(
"https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&piprop=original&titles="..wikititle, //请求维基百科的数据
"GET",
function(event)
local res2 = json.prettify( event.response ) //将响应数据进行格式化处理
local decoded2 = json.decode( res2 ) //解码json格式的数据
print( "Data2: " .. ( res2 ) ) //打印响应数据
print(decoded2.query.normalized[1].to) //打印数据中第一个normalized属性的to值
end,
params)
end
end
local headers = {}
headers["Content-Type"] = "application/json"
local body = ""
local params = {}
params.headers = headers
params.body = body
network.request("https://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=select+*%0D%0Awhere+{%3Fperson+rdfs%3Alabel+%3Fperson_name+%3B+rdf%3Atype+%3Ftype+%3B+dbo%3AbirthDate+%3Fbirthdate+.%0D%0A++++bind(rand(1+%2B+strlen(str(%3Fperson))*0)+as+%3Frid)%0D%0AFILTER+regex(%3Ftype%2C+%22Person%22)%0D%0A}+order+by+%3Frid%0D%0ALIMIT+10&format=text%2Fhtml&CXML_redir_for_subjs=121&CXML_redir_for_hrefs=&timeout=30000&debug=on&format=json", "GET", networkListener, params)
2017-08-18 10:21:48
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

在这种情况下,您需要使用嵌套回调。我还将把这种特殊情况拆分成自己的函数,而不是尝试使用一个庞大的函数来处理所有内容。在事件驱动的世界中,将匿名函数传递以执行工作是相当标准的,因为您无法完全控制事情发生的时间。
local function getWikipediaData(event) local res = json.prettify( event.response ) local decoded = json.decode( res ) if ( event.isError ) then print( "--网络错误-- ", ( res ) ) return end local dbpuri = decoded.results.bindings[1].person.value local wikititle = string.gsub(dbpuri, "http://dbpedia.org/resource/", "") network.request( "https://en.wikipedia.org/.../&titles=".. wikititle, "GET", function(event) -- 在这里处理维基百科的响应 end, params) end network.request( "https://dbpedia.org/sparql?...", "GET", getWikipediaData, params);