Lua - 如何将 API 调用获取的字符串传递到另一个 API 调用中?

在一个 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 的调用最好)。非常感谢!

点赞
用户1208078
用户1208078

在这种情况下,您需要使用嵌套回调。我还将把这种特殊情况拆分成自己的函数,而不是尝试使用一个庞大的函数来处理所有内容。在事件驱动的世界中,将匿名函数传递以执行工作是相当标准的,因为您无法完全控制事情发生的时间。

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);
2017-08-17 16:56:50
用户7665853
用户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