搜索
楼主: 鬼雨

lua-cjson库

  [复制链接]
发表于 2025-3-24 17:23:44 | 显示全部楼层
回复本帖可获得 18 金钱奖励! 每人限 2 次(中奖概率 80%)
发表于 2025-3-24 17:24:00 | 显示全部楼层
回复本帖可获得 18 金钱奖励! 每人限 2 次(中奖概率 80%)
发表于 2025-4-10 13:37:23 | 显示全部楼层


回复本帖可获得 18 金钱奖励! 每人限 2 次(中奖概率 80%)
发表于 2025-4-13 09:42:14 | 显示全部楼层
不错不错
发表于 2025-4-15 19:11:14 | 显示全部楼层
看不懂看不懂,这么难吗?
发表于 2025-4-15 20:27:25 | 显示全部楼层

local cjson = require("cjson.safe") -- 使用安全版

local data = {
    name = "HGE引擎",
    age = 18,
    score = 99.5,
    array = {1, 2, 3, 4, 5},
    dict = {
        name = "hello",
        age = 20,
        score = 10000
    }
}

-- 编码并处理错误
local json_str, encode_err = cjson.encode(data)
if not json_str then
    print("JSON Encode Error:", encode_err)
    return
end
print(json_str)

-- 解码并处理错误
local json_table, decode_err = cjson.decode(json_str)
if not json_table then
    print("JSON Decode Error:", decode_err)
    return
end

-- 安全访问字段(假设字段可能存在)
print(json_table.name or "N/A")
print(json_table.age or 0)
print(json_table.score or 0)
print(json_table.array[1] or "No array data")
print(json_table.dict and json_table.dict.name or "No dict name")
发表于 2025-4-17 12:20:41 | 显示全部楼层
发表于 2025-4-22 09:09:59 | 显示全部楼层
回复本帖可获得 18 金钱奖励! 每人限 2 次(中奖概率 80%)
发表于 2025-4-25 18:11:51 | 显示全部楼层

回复本帖可获得 18 金钱奖励! 每人限 2 次(中奖概率 80%)
发表于 2025-5-7 18:36:42 | 显示全部楼层
1111111111111111
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|HGEM2论坛

GMT+8, 2025-6-9 11:51 , Processed in 0.411014 second(s), 13 queries .

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表