Manipulating data from a JSON file

Hi guys!

I’m making an interactive storybook app and I have the image urls for each page located within a JSON file. I use the JSON data to populate each page. This is all working fine but now I am wondering how I can successfully manipulate the images once each page has been populated.

I want to be able to apply transitions to the images that I have loaded onto the page from the JSON file. (As well as add handlers, sprites etc) Whats the best way to approach this (for multiple unique pages) ? 

Thanks :slight_smile:

require “CiderDebugger”;
require “util”

local contentDataTable = {}
local currentPage = 1
local totalNumPages
local view = display.newGroup()

 
local function clearView()
    display.remove(view) – cleanup
    view = display.newGroup()
end

local function getImagesTable()
     local pageIndex = “page” … currentPage
     local pageTable = contentDataTable[pageIndex]
     return pageTable.images
end

 
local function updateView()
     clearView()
     local imagesTable = getImagesTable()
            for i = 1, #imagesTable do
                  local image = display.newImage(imagesTable[i].url, imagesTable[i].xPos, imagesTable[i].yPos)
                    view:insert(image)
end
end

 
function swipedEventFinished(phase)
    return phase == “ended”
end

 
function swipedRight(event)
     return event.x > (display.contentWidth / 2)
end

 

function moveToNextPage()
     if currentPage >= 1 and currentPage < totalNumPages then
     currentPage = currentPage + 1
end
end

 
function moveToPreviousPage()
    if currentPage > 1 and currentPage <= totalNumPages then
    currentPage = currentPage - 1
end
end

 
function touchHandler(event)
      if swipedEventFinished(event.phase) then
            if swipedRight(event) then

               moveToNextPage()

else
       moveToPreviousPage()
end
 

updateView() 

end
return true
end

function init()

 contentDataTable = loadTable(“pageData.json”)

 totalNumPages = length(contentDataTable)

updateView()
end

Runtime:addEventListener(“touch”, touchHandler)

 init()