[英]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
就個人而言,我只保存整個設置表。 保存未更改的值沒有什么害處。 如果您需要輔助表中的這些值,則只需將該表傳遞給json.encode(),然后寫出該JSON位即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.