Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deserialize a json array using xstream

There is a lot information at stackoverflow about how to deserialize a json array using Gson.

But how can I do the same using XStream with jettison?

Here is json:

{"entity":[{"id":"1", "name":"aaa"}, {"id":"2", "name":"bbb"}]}

Here is XStream code how I try to parse it:

XStream xstream = new XStream(new JettisonMappedXmlDriver());
xstream.alias("entity", Entity[].class);
return (Entity[])xstream.fromXML(jsonString);

I have following exception:

com.thoughtworks.xstream.converters.ConversionException: id : id
like image 800
Misha Avatar asked May 03 '26 19:05

Misha


1 Answers

As can be seen from this answer related to root element XStream fails when there is not root element in JSON.

Once you map entity to certain Java class XStream cannot find root element for pairs of id and name (as in JSON they are not enclosed in element).

Only hand-made wrapper, manipulating input streams or using custom converter can help here.

like image 85
flaz14 Avatar answered May 06 '26 09:05

flaz14