Questions about decoding JSON from Google Calendar [code inside]

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

hi, do u have a simple demo to show how to sync data from corona to calendar.google?

would be great

thanks

chris

Hi, I’m looking to do the calendar feature with data sync with google calena. It would be great if there is some demo. Thanks.

hi, do u have a simple demo to show how to sync data from corona to calendar.google?

would be great

thanks

chris

Hi, I’m looking to do the calendar feature with data sync with google calena. It would be great if there is some demo. Thanks.