Lua知识点2_使用json

以下是在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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值