cocos2d에서 Lua -- json 파일 해석

3798 단어 Lua

Date.lua


module(Data·package.seeaall) function getItemData(num1·num2) local itemData = ITEM[num1] [num2] return Iteall) function getItem Data(num1·num2) local itemData = ITEEM[num1] ITETEM[num1] [num1] [num1] [num1] [num1] [num2] return snail Datall Datal) function function function getItem Datal(nunction function get ItItem Datal(numn chapterBtnDataendfunction writeDocument(str) local docpath = cc.FileUtils:getInstance():getWritablePath().."data.json"print("bbbbb%s",docpath) if io.exists(docpath) == false then local f = io.open(docpath,"w")    print("wwwwww:%s",docpath)    f:write(str)    f:close() end endfunction readDocument(  ) local docpath = cc.FileUtils:getInstance():getWritablePath().."data.json"local str = cc.FileUtils:getInstance(): get StringFromFile(docpath) return strend는 function write Document Two(str) local docpath = cc를 재설정한 것으로 볼 수 있습니다.FileUtils:getInstance():getWritablePath().."data.json"local f = assert(io.open(docpath,"w"))    print("wwwwww:%s",docpath)    f:write(str)    f:close() endSCENE = {}SCENE[1] = {}SCENE[1][1] = {lock = 0, star = 0, num = 6}SCENE[1][2] = {lock = 1, star = 0, num = 11}SCENE[1][3] = {lock = 1, star = 0, num = 6}SCENE[1][4] = {lock = 1, star = 0, num = 12}SCENE[1][5] = {lock = 1, star = 0, num = 17}SCENE[1][6] = {lock = 1, star = 0, num = 16}SCENE[2] = {}SCENE[2][1] = {lock = 0, star = 0, num = 10}SCENE[2][2] = {lock = 1, star = 0, num = 11}SCENE[2][3] = {lock = 1, star = 0, num = 19}SCENE[2][4] = {lock = 1, star = 0, num = 12}SCENE[2][5] = {lock = 1, star = 0, num = 12}SCENE[2][6] = {lock = 1, star = 0, num = 16}SCENE[3] = {}SCENE[3][1] = {lock = 0, star = 0, num = 14}SCENE[3][2] = {lock = 1, star = 0, num = 16} SCENE[3] [3] = {lock = 1, star = 0, num = 11} SCENE[3] [4] = {lock = [4] = {lock = 11} SCENENE[3] [4] = {lock = 1, star = 0, num = 10[3] [3] = SCENE[3] [3] [3] = [3] = [3] = [3] = {lock = [3] [3] = [3] = [3] = {lock = [3] = [3] = [3] = {lock = [3] [3] = [3] = [3] =,pic2 = "house lock1.png"}CHAPTERBTN[2] = {pic = "house2.png",pic2 = "house lock2.png"}
CHAPTERBTN[3] = {pic = "house3.png", pic2 = "house_lock3.png"}
-- json 파일 구문 분석
  JSON  。
 :
jsonString = json.encode( )
  Data.writeDocument(json.encode(Data.SCENE) - 쓰기
local str = Data.readDocument() - 읽습니다.
  JSON  。
 :
table = json.decode(string)
local Table = json.decode(str, 1) - Table로 디코딩
for i=1,#Table[1] do
for k,v  in  pairs (Table[1][i]) do
print(v,k)
end
print(".........................")
end
실행 결과:
[LUA-print] 0 star
[LUA-print] 6 num
[LUA-print] 0 lock
[LUA-print] .........................
[LUA-print] 0 star
[LUA-print] 11 num
[LUA-print] 1 lock
[LUA-print] .........................
[LUA-print] 0 star
[LUA-print] 6 num
[LUA-print] 1 lock
[LUA-print] .........................
[LUA-print] 0 star
[LUA-print] 12 num
[LUA-print] 1 lock
[LUA-print] .........................
[LUA-print] 0 star
[LUA-print] 17 num
[LUA-print] 1 lock
[LUA-print] .........................
[LUA-print] 0 star
[LUA-print] 16 num
[LUA-print] 1 lock
[LUA-print] .........................
local str = Data. 수정readDocument()
local myTable = json.decode(str, 1)
myTable[self._Chapter][self._Case].star = self.starNum
if self._Case < 6 then
myTable[self._Chapter][self._Case+1].lock = 0
end
Data.writeDocumentTwo(json.encode(myTable))

좋은 웹페이지 즐겨찾기