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
With array I cannot get it running, but with a list:
Java:
package de.mosst.spielwiese;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import lombok.Data;
public class XStreamDeserializeJsonWithJettison {
@Test
@SuppressWarnings("unchecked")
public void smokeTest() {
InputStream file = XStreamDeserializeJsonWithJettison.class.getResourceAsStream("XStreamDeserializeJsonWithJettison.json");
XStream xStream = new XStream(new JettisonMappedXmlDriver());
xStream.processAnnotations(Entity.class);
List<Entity> entities = (List<Entity>) xStream.fromXML(file);
System.out.println(entities);
}
@Data
@lombok.AllArgsConstructor
@XStreamAlias("entity")
class Entity {
String id;
String name;
}
}
XML:
{
"list": [
{
"entity": [
{
"id": 1,
"name": "odin"
},
{
"id": 2,
"name": "dwa"
}
]
}
]
}