简体   繁体   English

在groovy中从rabbitmq接收json

[英]reciving json from rabbitmq in groovy

I have problem with parsing json provided by RabbitMq. 我在解析RabbitMq提供的json时遇到问题。 When I print Rabbit message i got: 当我打印Rabbit消息时,我得到:

{id=546456, userId=345345}

I need to parse this json with JsonSlurper but i got exception: 我需要使用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

It seems that quotes chars are missing. 似乎缺少引号字符。 How to parse it without errors? 如何解析没有错误? Thanks 谢谢

edit 编辑

When i send this message: 当我发送此消息时:

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

I recive this message in same form eg: 我以相同的形式接收此消息,例如:

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

But there is another exception: 但是还有另一个例外:

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

this is not valid JSON: {id=546456, userId=345345} 这是无效的JSON: {id=546456, userId=345345}

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

the JSON spec requires double-quotes for keys and string values. JSON规范要求键和字符串值使用双引号引起来。

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

note the double quotes around both keys. 请注意两个键周围的双引号。 You need to adjust your code to send your JSON documents properly formatted 您需要调整代码以发送格式正确的JSON文档

Sometimes, jq (the JSON Query processor) can be helpful with quasi-JSON input. 有时, jq (JSON查询处理器)对准JSON输入可能会有所帮助。 In the particular case you give, if the quasi-JSON text is put in a file named invalid.json: 在特定情况下,如果将准JSON文本放入名为invalid.json的文件中,则:

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

The above only works because in your case, substituting ":" for "=" blindly is satisfactory. 上面的方法之所以可行,是因为在您的情况下,用“:”代替“ =”是令人满意的。

Even if that is not the case, however, chances are that the combination of jq and some "text-wrangling" will do the job. 但是,即使事实并非如此,但jq和某些“文本整理”的组合很有可能会成功。

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

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