基本上,我将一台服务器上的marshall转换为JSON,然后将其发送到另一台服务器,在那里它应该是unmarshalled。我使用一个名为list wrapper的响应对象,这样如果有任何错误,我可以传递它们。随着下面由badgerfish(jettison)编组的JSON,在一个resteasy类中,然后返回到另一个服务器,GSON将解组为listwrapper对象,但是里面的列表为null。有任何想法吗? 注意:列表必须保持通用,因为不同的对象可能会进入列表,尽管列表中一次只能包含一种类型。
的Json
解组
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
Object List;
if (!JSON.equals("")) {
List = gson.fromJson(new BufferedReader(new StringReader(JSON)), ListWrapper.class);
}
Listwrapper类型
package com.airit.fids.ejbinterface.resteasy;
@XmlRootElement(name = "ListWrapper")
/**
*
* @author ddouberley
*/
public class ListWrapper {
private Vector<Object> objects;
private String status;
private int batch;
private ValidationException e;
public ListWrapper() {
this.setStatus("Success");
}
public ListWrapper(Vector<Object> list) {
this.setStatus("Success");
this.objects = list;
}
public ListWrapper(int x) {
this.setStatus("batch");
this.batch = x;
}
public Vector<Object> getList() {
return objects;
}
public void setList(Vector<Object> object) {
this.objects = object;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@XmlJavaTypeAdapter(ThrowableAdapter.class)
public ValidationException getE() {
if (e != null) {
return e;
} else {
return null;
}
}
public void setE(ValidationException x) {
this.e = x;
}
public int getBatch() {
return batch;
}
public void setBatch(int batch) {
this.batch = batch;
}
}
答案 0 :(得分:1)
通过查看您的ListWrapper
课程以及您发布的json
课程,您可能会出现不匹配问题,这可能是导致其无法解散的原因。
有关:
{"ListWrapper":{"batch":{"$":"0"},"list":{"@xmlns":{"xsi":"http:\/\/www.w3.org\/2001\/XMLSchema-instance"},"@xsi:type":"fidsUsers","createdBy":{"$":"administrator"},"createdWhen":{"$":"2013-02-25T17:29:19-05:00"},"endDate":{"$":"2016-10-28T00:00:00-04:00"},"isDisabled":{"$":"N"},"previousPasswords":{"$":"HXQDa4WxTdBmZtvhMVTgnw==@@@@zW6bdHkKdMN2p6CgRNjNHA==@@@@Sim7JN3kaHoXnh3KUS2++Q==@@@@Emz7zU0Wrm0lyb\/K522O5A==@@ZirxzRl28JqfjOzIaMzAog=="},"primaryKey":{"$":"David"},"pswdChgDate":{"$":"2013-07-12T08:27:46-04:00"},"pswdCount":{"$":"0"},"roleId":{"$":"Admin"},"roleIdFidsRoles":{"globalAccess":{"$":"Y"},"primaryKey":{"$":"Admin"},"roleDesc":{"$":"Administrator"},"roleId":{"$":"Admin"},"updatedBy":{"$":"David"}},"startDate":{"$":"1992-07-28T00:00:00-04:00"},"updatedBy":{"$":"David"},"updatedWhen":{"$":"2013-10-02T10:46:31-04:00"},"userId":{"$":"David"},"userName":{"$":"David3"},"userPassword":{"$":"HXQDa4WxTdBmZtvhMVTgnw=="}},"status":{"$":"Success"}}}
对我而言object ListWrapper
有3个名为:batch,list和status的字段,其中batch是一个对象,其中一个字段是数字,list是一个map,status是一个有一个的对象在其中的字符串。
我不熟悉badgerfish,在你的ListWrapper
尝试将private Vector<Object> objects
更改为private Map<String,Object> objects
(当然还有匹配的getter / setter)并看看是否有效
答案 1 :(得分:1)
列表必须保持通用,因为可能会有不同的对象 列表,虽然列表中只有一个类型 时间。
如果您在解组之前知道它是什么类型,那么它只能通过使用TypeToken
来工作示例代码:
Type fooType = new TypeToken<Foo<Bar>>() {}.getType();
gson.toJson(foo, fooType);
gson.fromJson(json, fooType);
此外,定义将更改为:
public class ListWrapper {
private Vector<T> objects;
private String status;
private int batch;
}
或
public class ListWrapper {
private Vector<T extends someBaseObject> objects;
private String status;
private int batch;
}
取决于您决定实施的具体程度。
答案 2 :(得分:1)
你不能直接使用ListWrapper
反序列化,你需要一个容器类,这就是你列出为空的原因。顺便说一下,你的列表不是列表而是地图(花括号限制list
的内容)。
我创建了一个正确解析JSON的代码,并为您提供了一些提取数据的简单功能。由于您希望保持在服务器之间传递的数据的灵活性,因此您需要字符串来访问数据。
以下是准备复制和粘贴的代码,供您自己试用。请记住,访问器方法基于您在示例中显示的结构。我为您提供了可以从中提取的主要4种不同类型的数据。如果您需要更多相关信息,请与我们联系。
package stackoverflow.questions.q19817221;
import java.util.*;
import com.google.gson.Gson;
public class Q19817221 {
public class ListWrapper {
private Map list;
private Map status;
private Map batch;
private Object extractValue(Map m) {
return m.get("$");
}
public Integer getBatch() {
return Integer.valueOf( (String) extractValue(batch));
}
public Object getValueFromList(String key) {
try {
Map m = (Map) list.get(key);
if (m != null)
return extractValue(m);
} catch (Exception e) {
return list.get(key);
}
return null;
}
public Object getValueFromList(String secondLevelKey, String key) {
Map secondLevelMap = (Map) list.get(secondLevelKey);
try {
Map m = (Map) secondLevelMap.get(key);
if (m != null)
return extractValue(m);
} catch (Exception e) {
return list.get(key);
}
return null;
}
}
public class Container {
public ListWrapper ListWrapper;
}
/**
* @param args
*/
public static void main(String[] args) {
String json = "{\"ListWrapper\":{\"batch\":{\"$\":\"0\"},\"list\":{\"@xmlns\":{\"xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},\"@xsi:type\":\"fidsUsers\",\"createdBy\":{\"$\":\"administrator\"},\"createdWhen\":{\"$\":\"2013-02-25T17:29:19-05:00\"},\"endDate\":{\"$\":\"2016-10-28T00:00:00-04:00\"}," +
"\"isDisabled\":{\"$\":\"N\"},\"previousPasswords\":{\"$\":\"HXQDa4WxTdBmZtvhMVTgnw==@@@@zW6bdHkKdMN2p6CgRNjNHA==@@@@Sim7JN3kaHoXnh3KUS2++Q==@@@@Emz7zU0Wrm0lyb/K522O5A==@@ZirxzRl28JqfjOzIaMzAog==\"}," +
"\"primaryKey\":{\"$\":\"David\"},\"pswdChgDate\":{\"$\":\"2013-07-12T08:27:46-04:00\"},\"pswdCount\":{\"$\":\"0\"},\"roleId\":{\"$\":\"Admin\"},\"roleIdFidsRoles\":{\"globalAccess\":{\"$\":\"Y\"},\"primaryKey\":{\"$\":\"Admin\"},\"roleDesc\":{\"$\":\"Administrator\"},\"roleId\":{\"$\":\"Admin\"}," +
"\"updatedBy\":{\"$\":\"David\"}},\"startDate\":{\"$\":\"1992-07-28T00:00:00-04:00\"},\"updatedBy\":{\"$\":\"David\"},\"updatedWhen\":{\"$\":\"2013-10-02T10:46:31-04:00\"},\"userId\":{\"$\":\"David\"},\"userName\":{\"$\":\"David3\"},\"userPassword\":{\"$\":\"HXQDa4WxTdBmZtvhMVTgnw==\"}},\"status\":{\"$\":\"Success\"}}}";
Container c = new Gson().fromJson(json, Container.class);
ListWrapper lw = c.ListWrapper;
System.out.println("batch:" + lw.getBatch());
System.out.println("createdBy:" + lw.getValueFromList("createdBy"));
System.out.println("@xsi:type: " + lw.getValueFromList("@xsi:type"));
System.out.println("roleIdFidsRoles\\primaryKey: " + lw.getValueFromList("roleIdFidsRoles", "primaryKey"));
}
}
这是执行结果:
batch:0
createdBy:administrator
@xsi:type: fidsUsers
roleIdFidsRoles\primaryKey: Admin