简体   繁体   中英

Parsing JSON from Google Distance Matrix API with Corona SDK

So I'm trying to pull data from a JSON string (as seen below). When I decode the JSON using the code below, and then attempt to index the duration text , I get a nil return. I have tried everything and nothing seems to work.

Here is the 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:

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 ). The solution is simple:

"JSON and Lua tables are almost identical data structures. In this case your table data2 has top level entries:

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:

data.rows[1]

Then inside of it is another numerically indexed table called 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."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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