在一个Lua文件中,我想调用两个API并同时从两个API中获取数据。到目前为止,我设法调用一个API并打印数据,但不知道如何调用第二个调用,使用从第一个API调用获取的字符串。Lua - 如何传递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("--Network error-- ", (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("--Network error-- ", (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调用的代码)。非常感谢!
太棒了!它工作正常。非常感谢你! – user7665853