0

我从 xsd 生成了 JAXB 类,并使用 ObjectMapper 将 JAXB 类转换为 json。但在我的 JSON 中,我想要最上面的字段名称。在 ObjectFactory 类中显示的名称 @XmlElementDecl(namespace = "", name = "Foo") name ie My Json sould 看起来像这样

{
"Foo":{
       "Foo_Inside": {
                      "FirstName":"abc",
                      "LastName","xyz"
                     },
      }
}

下面是我用来将对象转换为 json 的代码

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JaxbAnnotationModule());
String jsonString = mapper.writeValueAsString(myJaxBObject);

当我从 XSD 生成 JAXB 类时,我只创建了类,直到 Foo_Inside。我是这个概念的新手,任何人都可以帮助我生成包含 TopMost 键名的 JSON,即 Foo 在我的例子中。

4

1 回答 1

0

好的,这将基于一个假设,因为没有提供 xsd。我相信你有一个看起来像这样的 xsd:

<xsd:element name="Foo" type="Foo_Inside"/>

<!-- and somewhere in the schema also have -->
<xsd:complexType name="Foo_Inside">
    <xsd:sequence>
        <xsd:element name="blah" type="BlahType"/>
        <xsd:element name="blah-blah" type="BlahBlahType" />
    </xsd:sequence>
</xsd:complexType>

您需要做的是删除上面指定的两个并替换为:

<xsd:element name="Foo">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="blah" type="BlahType"/>
            <xsd:element name="blah-blah" type="BlahBlahType" />
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

它将根据您的需要生成您的 Foo 类作为@XmlRootElement

于 2018-02-07T07:48:12.783 回答