I’m using XStream to map some JSON back into Java objects. I’ll be dealing with a JSON string that might look like this:
{
"widgets":[
{
"widget_name": "Kenny",
"widget_type": "Character"
},
"widget_name": "Apple",
"widget_type": "Fruit"
}
]
}
Update: I believe this JSON could be interpreted as:
This JSON represents an un-named object that contains a
widgetsobject. Thewidgetsobject, in turn, contains an array of other un-named objects. Each of these un-named objects contain two properties:widget_nameandwidget_type.
Each Widget in this widgetlist corresponds to a POJO:
public class Widget {
private String name;
private String type;
// ...etc.
}
I’d like the above JSON string to map back to a List<Widget>, but I can’t seem to figure out how to alias the class correctly:
XStream xs = new XStream();
xs.alias("widgetlist", List.class); // Not a List<Widget> !
How can I get the xs mapper to translate widgetlist into a List<Widget>? Thanks in advance.
This piece of code worked for me, however the JSON generated is not similar to the one you have. If you were to use the
JsonHierarchicalStreamDriverinstead of theJettisonMappedXmlDriveryou would get the same JSON as you have in your question. Downside of this is that theJsonHierarchicalStreamDrivercannot read JSON and will give anUnsupportedOperationExceptionif you try.The generated XML by
JettionMappedXmlDriverInterpretation
The JSON is the
DROP_ROOT_MODEversion of the below JSON(un-named object, if you like).You can therefore look at it as an un-named object that contains a list of
Widget. TheWidgetobject still have the same structure as described in the question.Xstream settings
JSON Output