简体   繁体   English

使用Corona SDK从Google Distance Matrix API解析JSON

[英]Parsing JSON from Google Distance Matrix API with Corona SDK

So I'm trying to pull data from a JSON string (as seen below). 所以我试图从JSON字符串中提取数据(如下所示)。 When I decode the JSON using the code below, and then attempt to index the duration text , I get a nil return. 当我使用下面的代码decode JSON ,然后尝试索引duration text ,我得到一个零返回。 I have tried everything and nothing seems to work. 我已经尝试了一切,似乎没有任何工作。

Here is the Google Distance Matrix API JSON : 以下是Google Distance Matrix API JSON

{
    "destination_addresses" : [ "San Francisco, CA, USA" ],
    "origin_addresses" : [ "Seattle, WA, USA" ],
    "rows" : [
    {
        "elements" : [
        {
            "distance" : {
                "text" : "1,299 km",
                "value" : 1299026
            },
            "duration" : {
                "text" : "12 hours 18 mins",
                "value" : 44303
            },
            "status" : "OK"
        }]
    }],
    "status" : "OK"
}

And here is my code: 这是我的代码:

local json = require ("json")
local http = require("socket.http")
local myNewData1 = {}
local SaveData1 = function (event)

distanceReturn = ""
distance = ""

local URL1 = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Seattle&destinations=San+Francisco&mode=driving&&sensor=false"
local response1 = http.request(URL1)
local data2 = json.decode(response1)

if response1 == nil then
    native.showAlert( "Data is nill", { "OK"})
    print("Error1")
    distanceReturn = "Error1"

elseif data2 == nill then
    distanceReturn = "Error2"
    native.showAlert( "Data is nill", { "OK"})
    print("Error2")         
else
    for i = 1, #data2 do
        print("Working")  
        print(data2[i].rows)

        for j = 1, #data2[i].rows, 1 do
            print("\t" .. data2[i].rows[j])

            for k = 1, #data2[i].rows[k].elements, 1 do
                print("\t" .. data2[i].rows[j].elements[k])

                for g = 1, #data2[i].rows[k].elements[k].duration, 1 do                         
                    print("\t" .. data2[i].rows[k].elements[k].duration[g])

                    for f = 1, #data2[i].rows[k].elements[k].duration[g].text, 1 do
                        print("\t" .. data2[i].rows[k].elements[k].duration[g].text)

                        distance = data2[i].rows[k].elements[k].duration[g].text
                        distanceReturn = data2[i].rows[k].elements[k].duration[g].text

                    end
                end
            end 
        end 
    end
end

timer.performWithDelay (100, SaveData1, 999999)

Your loops are not correct. 你的循环不正确。 Try this shorter solution. 试试这个更短的解决方

Replace all your "for i = 1, #data2 do" loop for this one below: 替换下面的所有“for i = 1,#data2 do”循环:

print("Working")  

for i,row in ipairs(data2.rows) do
    for j,element in ipairs(row.elements) do
        print(element.duration.text)
    end
end

This question was solved on Corona Forums by Rob Miracle ( http://forums.coronalabs.com/topic/47319-parsing-json-from-google-distance-matrix-api/?hl=print_r#entry244400 ). Rob Miracle在Corona论坛上解决了这个问题( http://forums.coronalabs.com/topic/47319-parsing-json-from-google-distance-matrix-api/?hl=print_r#entry244400 )。 The solution is simple: 解决方案很简单:

"JSON and Lua tables are almost identical data structures. In this case your table data2 has top level entries: “JSON和Lua表几乎是相同的数据结构。在这种情况下,您的表data2具有顶级条目:

data2.destination_addresses
data2.origin_addresses
data2.rows
data2.status

Now data2.rows is another table that is indexed by numbers (the [] brackets) but here is only one of them, but its still an array entry: 现在data2.rows是另一个由数字([]括号)索引的表,但这里只有其中一个,但它仍然是一个数组条目:

data.rows[1]

Then inside of it is another numerically indexed table called elements. 然后在它内部是另一个名为elements的数字索引表。 So far to get to the element they are (again there is only one of them 到目前为止,他们得到的元素(再次只有他们中的一个

data2.rows[1].elements[1]

then it's just accessing the remaining elements: 然后它只是访问剩余的元素:

data2.rows[1].elements[1].distance.text
data2.rows[1].elements[1].distance.value
data2.rows[1].elements[1].duration.text
data2.rows[1].elements[1].duration.value

There is a great table printing function called print_r which can be found in the community code which is great for dumping tables like this to see their structure." 有一个很棒的表打印功能叫做print_r,它可以在社区代码中找到,这对于转储像这样的表看它们的结构非常有用。“

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM