以下是在cocos2dx-3.10 lua中
1.概述
Lua中json的所有函数都在以下文件中:
json.lua
2.实例
-- json.lua在本目录下
json = require('json')
-- Object to JSON encode
test = {
one='first', two='second', three={2,3,5}
}
jsonTest = json.encode(test)
print('JSON encoded test is: ' .. jsonTest)
-- JSON encoded test is: {"one":"first","three":[2,3,5],"two":"second"}
-- Now JSON decode the json string
result = json.decode(jsonTest)
print ("The decoded table result:")
table.foreach(result,print)
print ("The decoded table result.three")
table.foreach(result.three, print)
-- 测试直接的json字符串, 不按字串中的顺序输出排列.
json_str = '{"A":1, "B":2, "C":3, "D":4}'
result = json.decode(json_str)
for k,v in pairs(result) do
print(k..":"..v)
end
运行结果:
JSON encoded test is: {"one":"first","three":[2,3,5],"two":"second"}
The decoded table result:
three table: 0x8076ef8
one first
two second
The decoded table result.three
1 2
2 3
3 5
A:1
D:4
C:3
B:2
1.概述
Lua中json的所有函数都在以下文件中:
json.lua
2.实例
-- json.lua在本目录下
json = require('json')
-- Object to JSON encode
test = {
one='first', two='second', three={2,3,5}
}
jsonTest = json.encode(test)
print('JSON encoded test is: ' .. jsonTest)
-- JSON encoded test is: {"one":"first","three":[2,3,5],"two":"second"}
-- Now JSON decode the json string
result = json.decode(jsonTest)
print ("The decoded table result:")
table.foreach(result,print)
print ("The decoded table result.three")
table.foreach(result.three, print)
-- 测试直接的json字符串, 不按字串中的顺序输出排列.
json_str = '{"A":1, "B":2, "C":3, "D":4}'
result = json.decode(json_str)
for k,v in pairs(result) do
print(k..":"..v)
end
运行结果:
JSON encoded test is: {"one":"first","three":[2,3,5],"two":"second"}
The decoded table result:
three table: 0x8076ef8
one first
two second
The decoded table result.three
1 2
2 3
3 5
A:1
D:4
C:3
B:2