简体   繁体   中英

Corona SDK Lua, how to write to a specific point in a json file

I have a JSON file that I am using two store values for settings such and themes. I have managed to get it so that elements in my application use the values in the JSON file.

However, I am not trying to write to that file so that when the application is launched again it will use the new values.

savedSettings.json

{ 
    "settings" : {
        "theme" : {
            "background" : {
                "R" : 255, 
                "G" : 255,
                "B" : 255
            },
            "text" : {
                "R" : 75, 
                "G" : 75,
                "B" : 75
            },
            "accent" : {
                "R" : 192, 
                "G" : 148,
                "B" : 204
            }
        },
        "assists" : {
            "highlightDigits" : true,
            "remainingDigits" : true 
        }
    }
}

settings.lua

local function RemainingSwitchPress( event )
    local switch = event.target
    print( "Switch with ID '"..switch.id.."' is on: "..tostring(switch.isOn) )
end

I would like the RemainingSwitchPress to set the remainingDigits value to true/false depending on its state.

I have tried to use the following code but I don't see how I change that one value.

local JsonStorage = {}

-- Function to save a table.  Since game settings need to be saved from session to session, we will
-- use the Documents Directory
JsonStorage.saveTable = function(t, filename)
    local path = system.pathForFile( filename, system.DocumentsDirectory)
    local file = io.open(path, "w")

    if file then
        local contents = Json.encode(t)
        file:write( contents )
        io.close( file )
        return true
    else
        return false
    end
end

Personally, I would just save the entire settings table. It doesn't hurt to save the unchanged values. If you need those values in the assists table, then just pass that table to json.encode() and then write out that bit of JSON.

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