【问题标题】:Can I convert JSON object to JAXB object?我可以将 JSON 对象转换为 JAXB 对象吗?
【发布时间】:2014-10-07 07:46:57
【问题描述】:

我一直在使用 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;
    }

}

`

【问题讨论】:

    标签: json parsing jaxb


    【解决方案1】:

    “我可以将 JSON 对象转换为 JAXB 对象吗?”

    Jacksonjackson-module-jaxb-annotations 将允许您执行此操作。

    此 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注册JAXB模块

    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


    【讨论】:

    • 但问题是我没有特定的 id 名称和所有。我只有 skin1,它的数据,skin2 和它的数据等等。skin1 skin2..是不同的不同 id。这个实现是在任何地方都找不到。请你帮我实现这个
    • 我们至少需要查看您正在使用的 JAXB 类,或者我们可以从中生成 JAXB 类的 xsd
    • 这是我的一个类文件。 @XmlRootElement(name = "application") public class Application 实现 Serializable { private ArrayList containers;私人皮肤皮肤=新皮肤(); @XmlElement(name = "container") public ArrayList getContainers() { 返回容器; }//这样的setter和getter }和skins是另一个用于检索所有皮肤的类,每个皮肤的den skin类
    • 你能不能用它来编辑你的原始帖子,而不是作为评论
    • 您可能需要重新格式化 json 的发送方式。这个要求允许吗?
    猜你喜欢
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多