问题描述
在 ESP-12S 上使用 NodeMCU(最新版本)
我正在尝试解析用户提供的 JSON 并对其进行处理。 但是,由于 JSON 是用户提供的,我无法保证其有效性。所以我想先检查输入的 JSON 是否格式错误,然后再继续。
我使用了以下代码:
function validatejson(input)
if sjson.decode(input) then
return true
end
end
一个成功的例子是:
x = '{"hello":"world"}'
print(validatejson(x))
--> true
一个不成功的例子是:
x = '{"hello":world"}'
print(validatejson(x))
--> nil
上述函数可以工作,但是,在我编译的代码中使用它时,它会遇到 PANIC 错误并重新启动:
PANIC: unprotected error in call to Lua API: Incomplete JSON object passed to sjson.decode
因此,正如您可能已经做过的那样,我决定使用 pcall()
函数,该函数将错误返回为布尔值(false 表示调用中没有错误):
function validatejson(input)
if not pcall(sjson.decode(input)) then
return true
end
end
仍然没有运气! :(
任何想法如何使用 NodeMCU 在 Lua 中成功检测格式错误的 JSON?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)