【问题标题】:Corona SDK Lua, how to write to a specific point in a json fileCorona SDK Lua,如何写入json文件中的特定点
【发布时间】:2019-05-09 22:38:21
【问题描述】:

我有一个 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( "Switch with ID '"..switch.id.."' is on: "..tostring(switch.isOn) )
end

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

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

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

【问题讨论】:

  • 我推荐你使用来自 Simple-Table-Load-Save-Functions-for-Corona-SDK github 项目的 loadsave 库。

标签: json lua coronasdk


【解决方案1】:

就我个人而言,我只想保存整个设置表。保存未更改的值并没有什么坏处。如果您需要辅助表中的这些值,则只需将该表传递给 json.encode(),然后写出那部分 JSON。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 2011-08-03
    相关资源
    最近更新 更多