无法封送Java JAXB中的LinkedTreeMap

时间:2019-06-05 05:04:55

标签: java json xml jaxb gson

我正在将json响应数组转换为我的Affiliation对象,然后将其转换为XML

下面是将json解析为Object的代码

JsonArray json = parser.parse(jsonresponse).getAsJsonArray();  
Gson gson = new Gson();

return gson.fromJson(json, new TypeToken<ArrayList<T>>(){}.getType());

我能够以List个对象的PersonalAffiliation个对象的形式获得结果,而每个PersonalAffiliation个对象都是链接树形图

public class Affliation {

private List<Personaffiliation> personalaffiliation = new ArralyList<>();

public List<Personaffiliation> getPersonaffiliation() {
  return this.personalaffiliation;
}

public void setPersonaffiliation(List<Personaffiliation> personalaffiliation) {
  this.personalaffiliation = personalaffiliation
}

}

public class Personaffiliation {

private String title;
private String Name;

public void setTitile(String title) {
  this.title = title 
}
public String getTitle() {
  return this.title
}
public void setName(String name) {
  this.name = name 
}
public String getName() {
  return this.name
}   

}

下面的输出是每当我使用xstream之类的Xtsream.toXML(affiliation)

<com.example.affiliation>
<personaffiliation>
<com.google.gson.internal.LinkedTreeMap resolves to =“linked hash-map”>
<entry> 
<string>title</string>
<string>Mr.</string>    
</entry>
<entry> 

<string>Name</string>
<string>XYZ</string>    
</entry>
<com.google.gson.internal.LinkedTreeMap>
</personaffiliation>
<personaffiliation>
<com.google.gson.internal.LinkedTreeMap resolves to =“linked hash-map”>
<entry> 
<string>title</string>
<string>Mrs.</string>   
</entry>
<entry> 
<string>Name</string>
<string>ABC</string>    
</entry>
<com.google.gson.internal.LinkedTreeMap>
</personaffiliation>
</com.example.affiliation>

但是我想要的输出是

<com.example.affiliation>
<personaffiliation>
<title>Mr.</title>
<name>XYZ</name>    
</personaffiliation>
<personaffiliation>
<title>Mrs.</title>
<name>ABC</name>            
</personaffiliation>
</com.example.affiliation>

我尝试使用JAXB(jaxbMarshaller.marshal(affiliation, System.OUT);),但没有运气;我收到以下错误

javax.xml.bind.JAXBException: class com.google.gson.internal.LinkedTreeMap nor any of its super class is known in this context

请帮助。

0 个答案:

没有答案