I’ve been trying to use XStream to generate JSON to be consumed by Dojo. But I can’t find the way to generate the right JSON from XStream, it keeps adding extraneous {} around. I even tried this but no luck . For example I want to generate this JSON output (taken from Dojo FilteringSelect example) :
{"identifier":"abbreviation", "items": [ {"name":"Alaska", "label":"Alaska","abbreviation":"AK"}, {"name":"Wyoming", "label":"Wyoming","abbreviation":"WY"} ]} My attempt usign XStream:
package com.rubenlaguna.json; import java.util.ArrayList; import java.util.Collection; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver; class ItemCollection { public String identifier; public Collection<Item> items = new ArrayList<Item>(); public ItemCollection(String identifier) { this.identifier = identifier; } public boolean add(Item arg0) { return items.add(arg0); } } class Item { public String name; public String label; public String abbreviation; public Item(String name, String label, String abbreviation) { this.name = name; this.label = label; this.abbreviation = abbreviation; } } public class WriteXStreamDojoTest { public static void main(String[] args) { ItemCollection itemCollection = new ItemCollection("abbreviation"); itemCollection.add(new Item("Alaska","Alaska","AK")); itemCollection.add(new Item("Wyoming","Wyoming","WY")); XStream xstream = new XStream(new JettisonMappedXmlDriver()); String erroneousJsonStr = xstream.toXML(itemCollection); System.out.println(erroneousJsonStr); } } resulted in the following JSON Output (i added some space and breaklines for readability):
...