Corona SDK Lua,如何在json文件中写入特定位置

我有一个JSON文件,我使用它来存储设置和主题等值。我已经成功地让我的应用程序中的元素使用JSON文件中的值。

但是,我现在正在尝试写入该文件以便在下次启动应用程序时使用新值。

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( “ID为'”..switch.id..”' 的开关状态为:”..tostring(switch.isOn) )
end

我希望RemainingSwitchPress根据其状态将“remainingDigits”值设置为true / false。

我尝试使用以下代码,但不知道如何更改那个值。

local JsonStorage = {}

-- 保存表的函数。由于游戏设置需要从会话保存到会话,我们将使用Documents目录
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
点赞
用户1870706
用户1870706
个人而言,我会保存整个设置表。保存未更改的值并不会有害。如果您需要将那些值放入辅助表中,那么只需将该表传递给 `json.encode()` 然后写出那一部分 JSON 即可。
2018-12-10 23:10:16