Corona SDK Lua,如何写入 json 文件中的特定点
Corona SDK Lua, how to write to a specific point in a json file
我有一个 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
就个人而言,我会保存整个设置 table。保存未更改的值并没有坏处。如果你需要辅助 table 中的那些值,那么只需将 table 传递给 json.encode() 然后写出 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( "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
就个人而言,我会保存整个设置 table。保存未更改的值并没有坏处。如果你需要辅助 table 中的那些值,那么只需将 table 传递给 json.encode() 然后写出 JSON.
的那个位