Using Json with Corona SDK

Hi Everybody,

I have a project and one of the requirements is including a JSON file and also using the physics engine. So I’m just looking for a little help on this, if possible. :blink:

I understand that JSON will keep the data so that it can be recalled if needed but will I have to remove the images from the main.lua file and just put in the require “json” to access the images in the external file?

Also will this clash with the physics engine and delta time? if so what is the best way to rectify this?

Any help would be much appreciated! :slight_smile:

It’s not really clear what you’re asking for … JSON is a file format that is used to store, e.g., Lua tables on disk.  “require json” will load the JSON-processing library so that your code can use it, but by itself that won’t do anything for you.

Take a look at Jonathan’s and Rob’s tutorials on this:

There shouldn’t be any conflict with the physics and delta-time modules – but remember that loading a given module doesn’t really do much for you, it’s all in how you use those modules and tie them together.

@jbp1 Thank you so much for the links about json. They’ve been helpful. I’m still having some trouble so i hope you don’t mind if I ask you a few more questions. (sorry for the long post)

This is what I have for the loading screen in JSON:

{

  “appcontents”: [

       {

      “url”: “Assets/titleBg.png”,

      “xPos”: 160,

      “yPos”: 512,

      “name”:“title”

    },

    {

      “url”: “Assets/button.png”,

      “xPos”: 160,

      “yPos”: 160,

      “name”:“button”

    },

    {

      “url”: “Assets/button-over.png”,

      “xPos”: 160,

      “yPos”: 160,

      “name”:“button”

    }

  ],

And this is what I have in the contentLoader.lua file:

function functionTable.loadTable(filename)

    local path= system.pathForFile(filename, system.ResourceDirectory)

    local contents=""

    local myTable= {}

    local file= io.open(path, “r”)

    

    if file then

        local contents= file:read("*a")

        local jsonContents= json.decode(contents)

        functionTable.appContentsData = jsonContents.appcontents --calls the separate parts of json

        io.close(file)

    end

    local displayObjectRef

 for i=1, #functionTable.appContentsData do

        print(functionTable.appContentsData[i].url)

        displayObjectRef = display.newImage(functionTable.appContentsData[i].url)

     displayObjectRef.anchorX=0

        displayObjectRef.anchorY=0

        displayObjectRef.x= functionTable.appContentsData[i].xPos

        print(i…"."…i)

        displayObjectRef.y= functionTable.appContentsData[i].yPos

        print(i…"."…i)

        displayObjectRef.name= functionTable.appContentsData[i].name

–        if functionTable.hudConfigData[i].name== “button” then

–            print(functionTable.hudConfigData[i].url)

–            displayObjectRef:addEventListener(“touch”, functionTable.touchHandler)

–        end

        functionTable.displayGroup : insert(displayObjectRef)     

    end

end

return functionTable

*************************

 It is working but every time I click on the button showing in the simulator the second button moves…see attached screenshot to see what I mean… Can you help? I want it work so that it transitions to scene 1 then scene 2 but having trouble doing that. 

It’s not really clear what you’re asking for … JSON is a file format that is used to store, e.g., Lua tables on disk.  “require json” will load the JSON-processing library so that your code can use it, but by itself that won’t do anything for you.

Take a look at Jonathan’s and Rob’s tutorials on this:

There shouldn’t be any conflict with the physics and delta-time modules – but remember that loading a given module doesn’t really do much for you, it’s all in how you use those modules and tie them together.

@jbp1 Thank you so much for the links about json. They’ve been helpful. I’m still having some trouble so i hope you don’t mind if I ask you a few more questions. (sorry for the long post)

This is what I have for the loading screen in JSON:

{

  “appcontents”: [

       {

      “url”: “Assets/titleBg.png”,

      “xPos”: 160,

      “yPos”: 512,

      “name”:“title”

    },

    {

      “url”: “Assets/button.png”,

      “xPos”: 160,

      “yPos”: 160,

      “name”:“button”

    },

    {

      “url”: “Assets/button-over.png”,

      “xPos”: 160,

      “yPos”: 160,

      “name”:“button”

    }

  ],

And this is what I have in the contentLoader.lua file:

function functionTable.loadTable(filename)

    local path= system.pathForFile(filename, system.ResourceDirectory)

    local contents=""

    local myTable= {}

    local file= io.open(path, “r”)

    

    if file then

        local contents= file:read("*a")

        local jsonContents= json.decode(contents)

        functionTable.appContentsData = jsonContents.appcontents --calls the separate parts of json

        io.close(file)

    end

    local displayObjectRef

 for i=1, #functionTable.appContentsData do

        print(functionTable.appContentsData[i].url)

        displayObjectRef = display.newImage(functionTable.appContentsData[i].url)

     displayObjectRef.anchorX=0

        displayObjectRef.anchorY=0

        displayObjectRef.x= functionTable.appContentsData[i].xPos

        print(i…"."…i)

        displayObjectRef.y= functionTable.appContentsData[i].yPos

        print(i…"."…i)

        displayObjectRef.name= functionTable.appContentsData[i].name

–        if functionTable.hudConfigData[i].name== “button” then

–            print(functionTable.hudConfigData[i].url)

–            displayObjectRef:addEventListener(“touch”, functionTable.touchHandler)

–        end

        functionTable.displayGroup : insert(displayObjectRef)     

    end

end

return functionTable

*************************

 It is working but every time I click on the button showing in the simulator the second button moves…see attached screenshot to see what I mean… Can you help? I want it work so that it transitions to scene 1 then scene 2 but having trouble doing that.