解析嵌套对象时lua-cjson解码错误 [英] lua-cjson decode error when parse nested object

查看:493
本文介绍了解析嵌套对象时lua-cjson解码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

s = cjson.decode(s)

s = cjson.decode(s)

我想像这样对语句进行解码,但出现Expected value but found invalid token at character 27错误.我尝试了如下另一条语句:

I want to decode the statement like this, but got Expected value but found invalid token at character 27 error. I tried another statement like below:

{状态代码":503,正文":"{"代码:1,"消息:"服务不可用}"}

{"status_code":503,"body":"{"code": 1, "message": "Service unavailable"}"}

{状态代码":503,正文":{代码":1,消息":服务不可用"}}

{"status_code":503,"body":{"code": 1, "message": "Service unavailable"}}

但是有相同的错误.看起来cjson不支持嵌套嵌套解码?还有其他解决方案吗?谢谢.

However there is same error. It looks like cjson does't support decode nested? Is there another solution? Thx.

推荐答案

像这样的JSON数据很容易转换为Lua代码:

JSON data like this is easy to convert to Lua code:

s=[[
{"status_code":503,"body":{"code": 1, "message": "Service unavailable"}}
]]

s=s:gsub('(".-"):','[%1]=')
t=load("return "..s)()
for k,v in pairs(t) do print(k,v) end
for k,v in pairs(t.body) do print(k,v) end

这篇关于解析嵌套对象时lua-cjson解码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆