将格式不一致的JSON字符串转换为对象

时间:2019-04-22 04:53:14

标签: json jackson gson

我将下面的JSON作为我的代码的String输入输入。由于字符串的格式不统一,因此克服了转义字符和引号的分组以读取字符串并将其转换为Java对象和子对象的问题

{"payload":{"details":"{\"source\":\"incor\",\"type\":\"build\",\"created\":\"1553855543108\",\"organization\":null,\"project\":null,\"application\":null,\"_content_id\":null,\"attributes\":null,\"requestHeaders\":{}}","content":"{\"project\":{\"name\":\"spinner\",\"lastBuild\":{\"building\":false,\"number\":0}},\"master\":\"IncorHealthCheck\"}","rawContent":null,"eventId":"bb357b79-069b-426d-8d21-8d04b06f5009"},"eventName":"city_spinner_events"}

到目前为止,我已经尝试使用Jackson的GSON尝试读取String并将其转换为对象和子对象。但是,我只能将顶级对象物化。由于转义字符和解析器对引号分组的误读,我在创建子对象时遇到问题。它会引发错误和异常。

预期的JSON如下所示,可以将其转换为object:

{"payload":{"details":{"source":"incor","type":"build","created":"1553855543108","organization":null,"project":null,"application":null,"_content_id":null,"attributes":null,"requestHeaders":{}},"content":{"project":{"name":"spinner","lastBuild":{"building":false,"number":0}},"master":"IncorHealthCheck"},"rawContent":null,"eventId":"bb357b79-069b-426d-8d21-8d04b06f5009"},"eventName":"city_spinner_events"}

1 个答案:

答案 0 :(得分:0)

尝试unescapeJava中的org.apache.commons.text.StringEscapeUtils

StringEscapeUtils.unescapeJava(str);