如何为使用JSON我新泽西州元帅嵌套列表?我得到空数组或一个元素的字典包含数组的数组 [英] How do I marshal nested lists as JSON using Jersey? I get an array of nulls or an array of one-element dictionaries containing an array
问题描述
我的工作,它使用泽西对象转换为JSON的项目。我希望能写出嵌套列表,像这样:
{数据:[[一,二,三化] [A,B,C]}
我想先再presented数据转换为&LT的对象; LinkedList的< LinkedList的<弦乐>>>和我想泽西只会做正确的事。以上是为空值的列表输出:
{数据:[NULL,NULL]}
读取嵌套的对象需要被包装后,我试过如下:
@XmlRootElement(NAME =富)
@XmlType(propOrder = {数据})
公共类Foo
{
私人收藏< FooData>数据=新的LinkedList< FooData>(); @XmlElement(NAME =数据)
公文集< FooData>的getData()
{
返回的数据;
} 公共无效addData(收集数据)
{
FooData D =新FooData();
对于(对象o:数据)
{
。d.getData()加(O == NULL(字符串)O:o.toString());
}
this.data.add(四);
} @XmlRootElement(NAME =FooData)
公共静态类FooData
{
私人收藏<串GT;数据=新的LinkedList<串GT;(); @XmlElement
公文集<串GT;的getData()
{
返回的数据;
}
}
}
这code输出什么下文,这更接近我想要的:
{数据:[{数据:一,二,三]},{数据:A,B , C]}]}
欲第一数据是列表的列表,而不是一个元素的字典的列表。我如何做到这一点?
下面是我的JAXBContentResolver:
@Provider
公共类JAXBContextResolver实现ContextResolver<&JAXBContext而GT;
{
私人的JAXBContext语境;
私人设置<班级<>>类型; //只有父类都在这里需要。嵌套类是隐含的。
保护类<> [] =类类别新的Class [] {}让Foo.class; 保护套装<串GT; jsonArray =新的HashSet<串GT;(1){
{
添加(数据);
}
}; 公共JAXBContextResolver()抛出异常
{
地图<弦乐,对象>道具=新的HashMap<弦乐,对象>();
props.put(JSONJAXBContext.JSON_NOTATION以外,JSONJAXBContext.JSONNotation.MAPPED);
props.put(JSONJAXBContext.JSON_ROOT_UNWRAPPING,Boolean.TRUE);
props.put(JSONJAXBContext.JSON_ARRAYS,JSONArray),其中;
this.types =新的HashSet<班级<>>(Arrays.asList(类类别));
this.context =新JSONJAXBContext(classTyes,道具);
} 公众的JAXBContext的getContext(类<>的objectType)
{
回报(types.contains(的objectType))?背景:空;
}
}
你试过球衣,JSON ??
添加新泽西州JSON到类路径中(或您的Maven依赖)
然后,使用这样的:
@Provider
公共类JAXBContextResolver实现ContextResolver<&JAXBContext而GT; { 私人最终的JAXBContext背景; 公共JAXBContextResolver()抛出异常{
this.context =新JSONJAXBContext(JSONConfiguration.natural()建(),package.of.your.model);
} 公众的JAXBContext的getContext(类<>的objectType){
返回语境;
}}
您只需要在您的ressources像这样(假设DetailProduit是要序列化对象和DetailProduit.java是JAXB标记和package.of.your.model)
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path(/ {code})
公共DetailProduit getDetailProduit(@PathParam(code)字符串code){
....你$ C $ C ........
}
I'm working on a project which uses Jersey to convert objects to JSON. I'd like to be able to write out nested lists, like so:
{"data":[["one", "two", "three"], ["a", "b", "c"]]}
The object I'd like to convert first represented data as a <LinkedList<LinkedList<String>>>, and I figured Jersey would just do the right thing. The above was output as a list of nulls:
{"data":[null, null]}
After reading that nested objects need to be wrapped, I tried the following:
@XmlRootElement(name = "foo")
@XmlType(propOrder = {"data"})
public class Foo
{
private Collection<FooData> data = new LinkedList<FooData>();
@XmlElement(name = "data")
public Collection<FooData> getData()
{
return data;
}
public void addData(Collection data)
{
FooData d = new FooData();
for(Object o: data)
{
d.getData().add(o == null ? (String)o : o.toString());
}
this.data.add(d);
}
@XmlRootElement(name = "FooData")
public static class FooData
{
private Collection<String> data = new LinkedList<String>();
@XmlElement
public Collection<String> getData()
{
return data;
}
}
}
That code outputs what's below, which is closer to what I want:
{"data":[{"data":["one", "two", "three"]},{"data":["a", "b", "c"]}]}
I want the first data to be a list of lists, not a list of one-element dictionaries. How do I achieve this?
Here's my JAXBContentResolver:
@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext>
{
private JAXBContext context;
private Set<Class<?>> types;
// Only parent classes are required here. Nested classes are implicit.
protected Class<?>[] classTypes = new Class[] {Foo.class};
protected Set<String> jsonArray = new HashSet<String>(1) {
{
add("data");
}
};
public JAXBContextResolver() throws Exception
{
Map<String, Object> props = new HashMap<String, Object>();
props.put(JSONJAXBContext.JSON_NOTATION, JSONJAXBContext.JSONNotation.MAPPED);
props.put(JSONJAXBContext.JSON_ROOT_UNWRAPPING, Boolean.TRUE);
props.put(JSONJAXBContext.JSON_ARRAYS, jsonArray);
this.types = new HashSet<Class<?>>(Arrays.asList(classTypes));
this.context = new JSONJAXBContext(classTyes, props);
}
public JAXBContext getContext(Class<?> objectType)
{
return (types.contains(objectType)) ? context : null;
}
}
Have you tried jersey-json ??
Add jersey-json to your classpath (or your maven dependencies)
Then use this :
@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
private final JAXBContext context;
public JAXBContextResolver() throws Exception {
this.context = new JSONJAXBContext(JSONConfiguration.natural().build(), "package.of.your.model");
}
public JAXBContext getContext(Class<?> objectType) {
return context;
}
}
You only need something like this in your ressources (supposing DetailProduit is your object you want to serialize and that DetailProduit.java is jaxb tagged and in package.of.your.model)
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{code}")
public DetailProduit getDetailProduit(@PathParam("code") String code) {
.... Your Code ........
}
这篇关于如何为使用JSON我新泽西州元帅嵌套列表?我得到空数组或一个元素的字典包含数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!