我正在将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
请帮助。