繁体   English   中英

在groovy中从rabbitmq接收json

[英]reciving json from rabbitmq in groovy

我在解析RabbitMq提供的json时遇到问题。 当我打印Rabbit消息时,我得到:

{id=546456, userId=345345}

我需要使用JsonSlurper解析此json,但出现异常:

groovy.json.JsonException: Unable to parse JSON object



     The current character read is '{' with an int value of 123
    expecting '}' or ',' but got current char 'i' with an int value of 105
    line number 1
    index number 1

似乎缺少引号字符。 如何解析没有错误? 谢谢

编辑

当我发送此消息时:

{'id':663558460,"userId":345345}

我以相同的形式接收此消息,例如:

{'id':663558460,"userId":345345}

但是还有另一个例外:

The current character read is ''' with an int value of 39
expecting '}' or ',' but got current char ''' with an int value of 39
line number 1
index number 1

这是无效的JSON: {id=546456, userId=345345}

这也不是: {'id':663558460,"userId":345345}

JSON规范要求键和字符串值使用双引号引起来。

这是有效的JSON: {"id":663558460,"userId":345345}

请注意两个键周围的双引号。 您需要调整代码以发送格式正确的JSON文档

有时, jq (JSON查询处理器)对准JSON输入可能会有所帮助。 在特定情况下,如果将准JSON文本放入名为invalid.json的文件中,则:

$ jq -n -f <(sed 's/=/:/g' invalid.json)
{
  "id": 546456,
  "userId": 345345
}

上面的方法之所以可行,是因为在您的情况下,用“:”代替“ =”是令人满意的。

但是,即使事实并非如此,但jq和某些“文本整理”的组合很有可能会成功。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM