Questions about decoding JSON from Google Calendar [code inside]

Hi all, 

I made a public test calendar to sneak some JSON from so I can try out syncing a calendar in a Corona app - I am able to download the unformatted data, but when I try to find a specific item, I get “returns nil value” errors. 

Here is the code (including URL and things I have tried) - NOTE: the JSON validates though an online tool. 

local json = require "json" local dataURL = "http://www.google.com/calendar/feeds/csnfcl6h9k9qh769t6rs105pto@group.calendar.google.com/public/full?alt=json" local function newDataListener(event) if (event.isError) then print ("Error!") else -- print ( data[1] ) print ( "RESPONSE: " .. event.response ) local data = json.decode(event.response) print(data.title[2]) --table.foreach(data,print) --local t = event.response --t = json.decode( t ) --if #t \> 0 then --print ("Number of records " .. tostring(#t) ) --print ( t[1]["content"] ) --else --print ("no records found") -- end end end network.request ( dataURL, "GET", newDataListener )

Thanks!

These lines return the correct values: 

       print("Hello: " … data.version)

       print("Hello: " … data.encoding)

anything after that gets a nil value error. Any idea why? Is it the way that Google formats these tables? I am new to json. 

So I am able to find the correct table now - everything was inside of “feed”{ so I simply called data.feed.title which gave me the address of the table - inside of that table the text I’m looking is given the variable “$t” - how can I call this in corona? I get compile errors with or without quotes. 

Thanks! 

Hi there,

If you want to access a table called myTable with a string index of “$t”, try myTable["$t"].

The notation myTable.myIndex is a shorthand for myTable[“myIndex”], but it only works if “myIndex” doesn’t have various special characters.  Since in your case it did, you have to use the myTable["$t"] notation.

Hope this helps.

  • Andrew

Hi Andrew, 

Thanks for the reply - I tried this:

       print (“This: “, data.feed.title[”$i”])

and get this:

This:     nil

hmmmm I wonder how other languages call this, or maybe the google api’s have a special way for reading this? There has got to be a way around this though.

Hi there,

That looks fine to me.  But I notice in your earlier post you said it was $t, whereas in your code above you used $i – which one is right?

Clearly a sign that it’s time for more coffee - Thank you so much for your help Andrew - it is MUCH appreciated. 

No problem!

Hi Andrew, I do have one more question that you might be able to answer - 

There are multiple instances of “title” that appear since there are multiple calendar entries. My question is how can I print the different ones? 

Right now, data.feed.title["$t"] returns the first one that says “Public test calendar”  - there are 2 more entries with “title” but they all fall under data.feed. 

the link to the json is here: JSON

and I used this for formatting: HERE

Hi there,

It looks like “entry” is an array, so you could loop through it like this:

[lua]

for index,entry in ipairs(data.feed.entry) do

   print(entry.title["$t"])

end

[/lua]

  • Andrew

Hi Andrew, 

I never thanked you for answering my second question - You definitely helped me out quite and bit and I think other Corona users can benefit from this as the documentation on pulling data like this from Google sources is rather slim. 

If there is any way I can support your studio like buying your apps or donating please let me know. 

Thanks,

Marc 

I have another question - this time it’s about storing the data to a table that I can use later to print out. 

  1. when ever I try to put the data ipairs reads into a table, it will print out the table address instead of the content. When I use the print(entry.title[“t]” I get the actual strings … is there a special way to store what ipairs reads into a table to use for formatting later? 

  2. Some of the information I want to pull is in another table inside of entry - if I want to call “link” I try print (entry.link.href) however it alway prints nil - is there another way to print this one? Will I have to put it in a separate loop? 

here is my code: 

calData = {} --require "sqlite3" --global variables --calendar = {} --calendarData = {} local json = require "json" local dataURL = "http://www.google.com/calendar/feeds/csnfcl6h9k9qh769t6rs105pto@group.calendar.google.com/public/full?alt=json&max-results=15" function newDataListener(event) if (event.isError) then native.showAlert( "Network Error", "No wi-fi or cellular connection found.", {"OK"} ) else data = json.decode(event.response) for index,entry in ipairs(data.feed.entry) do calData[index] = {entry.title["$t"]} print (calData[index]) end end end

Thanks for the help!!!

Hey Marc, no problem, happy to help!  If you’d like, you can check out my game called What the Block?! at http://itunes.com/apps/whattheblock.

For your question #1, It looks like the problem is in the line [lua]calData[index] = {entry.title["$t"]}[/lua].  The issue is that you’ve wrapped the right-hand side in braces, which creates a new table with one element.  That’s why when you print calData[index], it’s printing a table address.  Replace that line with just [lua]calData[index] = entry.title["$t"][/lua] (no braces) and you should be good.

For your question #2, the reason entry.link.href is nil is that entry.link is an array of links, not just one link.  So, if you wanted to access the first one, it would be entry.link[1].href, the second would be entry.link[2].href.  You could access all of them via a loop, like this:

[lua]

for index,entry in ipairs(data.feed.entry) do

    calData[index] = entry.title["$t"]

    print (calData[index])

    for linkIndex,link in ipairs(entry.link) do

       print(link.href)

    end

end

[/lua]

It looks like the links have different “types”, so maybe it’s only one of them that you’re interested in, in which case, in that new inner loop, you could check if link.type is what you want, and if it is, then store link.href wherever you want to use it later.

  • Andrew

These lines return the correct values: 

       print("Hello: " … data.version)

       print("Hello: " … data.encoding)

anything after that gets a nil value error. Any idea why? Is it the way that Google formats these tables? I am new to json. 

So I am able to find the correct table now - everything was inside of “feed”{ so I simply called data.feed.title which gave me the address of the table - inside of that table the text I’m looking is given the variable “$t” - how can I call this in corona? I get compile errors with or without quotes. 

Thanks! 

Hi there,

If you want to access a table called myTable with a string index of “$t”, try myTable["$t"].

The notation myTable.myIndex is a shorthand for myTable[“myIndex”], but it only works if “myIndex” doesn’t have various special characters.  Since in your case it did, you have to use the myTable["$t"] notation.

Hope this helps.

  • Andrew

Hi Andrew, 

Thanks for the reply - I tried this:

       print (“This: “, data.feed.title[”$i”])

and get this:

This:     nil

hmmmm I wonder how other languages call this, or maybe the google api’s have a special way for reading this? There has got to be a way around this though.

Hi there,

That looks fine to me.  But I notice in your earlier post you said it was $t, whereas in your code above you used $i – which one is right?

Clearly a sign that it’s time for more coffee - Thank you so much for your help Andrew - it is MUCH appreciated. 

No problem!