简体   繁体   中英

Parse json array with quotes around it

I'm calling 3rd party API and receiving as a response next string:

"[{\"name\":\"name\",\"id\":1}]"

As I see it's not valid json because it has quotes around it. Is it possible somehow to map it to java object with jackson , gson libraries?

Or anyway I should write my custom converter/deserializer?

You don't need a custom converter or deserializer. You could write one of course, but I wouldn't encourage you to do so. Be explicit about what is happening here, especially when you are working in a team. It's the other side that is at fault here, they are not outputting valid JSON.

With Jackson, deserialize their output this way:

ObjectMapper mapper = new ObjectMapper();
String json = theirOutput.substring(1, theirOutput.length - 1);
Object myObject = mapper.readValue(json, MyObject.clas);

Put some documentation above why you do it this way so everybody understands what's happening here. In my opinion this is a much cleaner solution than writing a custom converter or deserializer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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