• 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
    -     }
    - }
    ]]
  • 相关阅读:
    C# Socket UDP 案例
    精通MVC 3 框架Controller Extensibility
    python一些DEMO总结
    影响ASP.NET程序性能的因素
    Django实战
    EF 4.3 CodeBased 数据迁移演练
    jQuery Gallery Plugin在Asp.Net中使用
    Action Func委托+异步委托
    使用CGO封装Windows API
    每日一例,练就编程高手
  • 原文地址:https://www.cnblogs.com/wdlhao/p/5115464.html
Copyright © 2020-2023  润新知