懒人精灵 之 Lua 捕获 json解析异常 ,造成的脚本停止.

Time: 2024年2月8日20:21:17 by:MemoryErHero

1 异常代码

Expected value but found T_END at character 1

2 异常代码

Expected value but found T_OBJ_END at character 22

3 处理方案 - 正确 json 示范

while true do
	--Expected value but found T_END at character 1
    --Expected value but found T_OBJ_END at character 22
	local str = '{"success":0,"code":[]}'
	local err,tbl = pcall(function(str)return jsonLib.decode(str)end,str)
	print(err)
	print(tbl)
    sleep(1000)
end

懒人精灵 之 Lua 捕获 json解析异常 ,造成的脚本停止._第1张图片

4 处理方案 - 错误 json 示范

while true do
	--Expected value but found T_END at character 1
    --Expected value but found T_OBJ_END at character 22
	local str = '{"success":0,"code":[}'
	local err,tbl = pcall(function(str)return jsonLib.decode(str)end,str)
	print(err)
	print(tbl)
    sleep(1000)
end

懒人精灵 之 Lua 捕获 json解析异常 ,造成的脚本停止._第2张图片

你可能感兴趣的:(lua,json,junit)