lua -- encode and decode


json.encode

將表格數據編碼為 JSON 字符串。

格式:

jsonString = json.encode(表格對象)
用法示例:

local str = json.encode({a=1,b="ss",c={c1=1,c2=2},d={10,11},100})
echo(str) -- {"a":1,"b":"ss","c":{"c1":1,"c2":2},"d":[10,11],"1":100}
local str = json.encode({1,2,"3",{10,11}})
echo(str) -- [1,2,"3",[10,11]]
Note: table作為字典使用時,整型鍵值將被轉換為字符串鍵值

local str = json.encode({a=1,[5]=3})
echo(str) -- {"a":1,"5":3}
Note: table所有鍵值為整型時,會當作數組看待,空位將轉化為null

local str = json.encode({[3]=2,[5]=3})
echo(str) -- [null,null,2,null,3]
~~

json.decode

將 JSON 字符串解碼為表格對象。

格式:

table = json.decode(string)
用法示例:

local json = require("framework.shared.json")
local tb = json.decode('{"a":1,"b":"ss","c":{"c1":1,"c2":2},"d":[10,11],"1":100}')
dump(tb) --[[
- "<var>" = {
-     "1" = 100
-     "a" = 1
-     "b" = "ss"
-     "c" = {
-         "c1" = 1
-         "c2" = 2
-     }
-     "d" = {
-         1 = 10
-         2 = 11
-     }
- }
]]
local tb = json.decode('[1,2,"3",[10,11]]')
dump(tb) --[[
- "<var>" = {
-     1 = 1
-     2 = 2
-     3 = "3"
-     4 = {
-         1 = 10
-         2 = 11
-     }
- }
]]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM