我一直在使用JAXB对象。现在我的要求是使用JSON而不是XML。有人能用逻辑来帮助我吗?
我无法用json对象替换整个代码库。有没有办法将这些json转换为jaxb对象?
提前致谢。
的xml:
<skins>
<skin id="lineNormal" type="style" displayname="Skin">
<attributes datatype="string" value="one" name="bg_type" platform="general"/>
<attributes datatype="string" value="5c5c5c00" name="background_color" platform="general"/>
<attributes datatype="boolean" value="false" name="customcss" platform="general"/>
<attributes datatype="string" value="00000000" name="border_color" platform="general"/>
<attributes datatype="number" value="0" name="border_width" platform="general"/>
<attributes datatype="string" value="plain" name="border_style" platform="general"/>
<attributes datatype="number" value="0" name="border_radius" platform="general"/>
<attributes datatype="string" value="Line" name="wtype" platform="general"/>
</skin>
JSON:
{
"lineNormal": {
"wType": "Button",
"bg_type": "one",
"border_color": "00000000",
"border_radius": 0,
"border_style": "plain",
"border_width": 0,
"border_type": 0,
"font_color": "00000000",
"font_size": 100,
"font_weight": "normal",
"background_color : "xxx"
}
}
我从上面的xml中获取应用程序图的类文件是: `
@XmlRootElement(name = "application")
public class Application implements Serializable
{
private ArrayList <Container> containers;
private Skins skins = new Skins();
private String name;
private String id;
@XmlElement(name = "container")
public ArrayList <Container> getContainers() {
return containers;
}
/**
* Sets teh containers collection
* @param containers
*/
public void setContainers(ArrayList <Container> containers) {
this.containers = containers;
}
/**
*
* @return
*/
@XmlElement(name = "skins")
public Skins getSkins()
{
return skins;
}
/**
*
* @param skins
*/
public void setSkins(Skins skins)
{
this.skins = skins;
}
@XmlElement(name = "globals")
public Global getGlobal() {
return global;
}
public void setGlobal(Global global) {
this.global = global;
}
@XmlAttribute(name="id")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement(name = "attributes")
public ArrayList<Attributes> getAttributes() {
return attributes;
}`
//皮肤类是:: `
public class Skins implements Serializable{
private ArrayList <Skin> skins = new ArrayList();
@XmlElement(name = "skin")
public ArrayList<Skin> getSkinList()
{
return skins;
}
/**
*
* @param skins
*/
public void setSkinList(ArrayList<Skin> skins)
{
this.skins = skins;
}
}
`
答案 0 :(得分:2)
&#34;我可以将JSON对象转换为JAXB对象吗?&#34;
jackson-module-jaxb-annotations的Jackson将允许您执行此操作。
此Jackson扩展模块支持使用JAXB(javax.xml.bind)注释作为本机Jackson注释的替代方法。它通常用于更容易地重用与JAXB框架一起使用的现有数据bean来读写XML。
使用Maven,你只需要这种依赖(它将引入其他几个Jackson核心依赖项):
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>2.4.0</version>
</dependency>
具有此依赖关系,您只需要使用ObjectMapper
JaxbAnnotationModule module = new JaxbAnnotationModule();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(module);
然后,您可以将JSON读入JAXB对象。
RootObject root = objectMapper.readValue(json, RootObject.class);
这不是所有用例的可靠解决方案,因为该模块不支持所有JAXB注释。见here for supported annotations