[英]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.