当java中缺少方括号时,将json字符串转换为object

时间:2022-02-08 21:46:05

I am trying to parse a json string mentioned below,

我正在解析下面提到的json字符串,

"inventoryItems":{"fare":"299.00","ladiesSeat":"false","passenger":{"address":"xxx,Bangalore","age":"26","email":"xxxxxxxxx@gmial.com","gender":"MALE","idNumber":"123ABC","idType":"PAN_CARD","mobile":"9999999999","name":"abcd","primary":"true","title":"Mr"},"seatName":"27"}

The structure is there is an inventoryItems which is an array and inside that there is an passenger *array*, The problem is there are no square braces, since the array contains only one value in both inventoryItems and passenger. This malformed data is from a third party server so I cant correct them. While converting this to Object Jackson library is throwing an excception.

结构是有一个inventoryItems它是一个数组,里面有一个乘客*数组*,问题是没有方括号,因为数组在inventoryItems和passenger中只包含一个值。这个错误的数据来自第三方服务器,所以我无法更正它们。当将其转换为对象时,Jackson库会抛出异常。

My question is how to form a proper json string with square braces from the above so that jackson is able to convert it into java object?

我的问题是如何从上面形成一个合适的json字符串,这样jackson就能够将它转换成java对象了?

1 个解决方案

#1


1  

The DeserializationFeature ACCEPT_SINGLE_VALUE_AS_ARRAY should solve your problem (via [ObjectMapper.enable(...)) without a conversion of the string.

DeserializationFeature ACCEPT_SINGLE_VALUE_AS_ARRAY可以解决您的问题(通过[ObjectMapper.enable(…))),而无需对字符串进行转换。

#1


1  

The DeserializationFeature ACCEPT_SINGLE_VALUE_AS_ARRAY should solve your problem (via [ObjectMapper.enable(...)) without a conversion of the string.

DeserializationFeature ACCEPT_SINGLE_VALUE_AS_ARRAY可以解决您的问题(通过[ObjectMapper.enable(…))),而无需对字符串进行转换。