【问题标题】:XML wrapper JSON format issueXML 包装器 JSON 格式问题
【发布时间】:2013-11-02 19:40:54
【问题描述】:

我有这门课:

 class A {
      @XmlElement(name = "bees")
      @XmlElementWrapper(name="bee")
      public List<B> bees;
    }

然后我的 XML 看起来像:

<a>
  <bees>
    <bee>...</bee>
    <bee>...</bee>
  </bees>
</a>

但是通过 JSON 使用时:

{
  "bees": {
    "bee": [
      ....
    ]
  }
}

我需要:

  {
  "bees": {
    "bee": { .. }, "bee": { .. }, ..
  }
}

谁能帮忙?

【问题讨论】:

    标签: java xml json xml-parsing


    【解决方案1】:

    注意:我是EclipseLink JAXB (MOXy) 领导,也是JAXB (JSR-222) 专家组的成员。

    代替:

    {
      "bees": {
        "bee": { .. }, 
        "bee": { .. }, 
        ..
      }
    }
    

    我会推荐以下内容:

    {
       "bees" : [ 
           { .. }, 
           { .. },
           ..
       } ]
    }
    

    以下是如何使用 MOXy 根据您的映射生成此 JSON:

    import java.util.*;
    import javax.xml.bind.*;
    import javax.xml.transform.stream.StreamSource;
    import org.eclipse.persistence.jaxb.JAXBContextProperties;
    import org.eclipse.persistence.jaxb.MarshallerProperties;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            Map<String, Object> properties = new HashMap<String, Object>();
            properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
            properties.put(JAXBContextProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
            JAXBContext jc = JAXBContext.newInstance(new Class[] {A.class}, properties);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            StreamSource xml = new StreamSource("src/forum19560166/input.xml");
            A a = unmarshaller.unmarshal(xml, A.class).getValue();
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
            marshaller.marshal(a, System.out);
        }
    
    }
    

    更多信息

    【讨论】:

      【解决方案2】:

      使用这个tools;t 我遇到了你描述的同样的问题。我们用来让它工作的解决方案是将我们的 Maven 依赖项从 Jackson 更改为 json-lib。我们将此站点用作guide

      如果使用 maven,请添加对 pom 的依赖。

      <dependency>
              <groupId>net.sf.json-lib</groupId>
              <artifactId>json-lib</artifactId>
              <version>2.3</version>
              <type>jar</type>
              <classifier>jdk15</classifier>
              <scope>compile</scope>
          </dependency>
          <dependency>
              <groupId>org.apache.commons</groupId>
              <artifactId>commons-io</artifactId>
              <version>1.3.2</version>
              <type>jar</type>
              <scope>compile</scope>
          </dependency>
          <dependency>
              <groupId>xom</groupId>
              <artifactId>xom</artifactId>
              <version>1.1</version>
          </dependency>
        </dependencies>
      

      将此 xml 保存在示例文件 sample-xml.xml 中

          <?xml version="1.0" encoding="UTF-8"?>
          <important-data certified="true" processed="true">
            <timestamp>232423423423</timestamp>
            <authors>
              <author>
                <firstName>Tim</firstName>
                <lastName>Leary</lastName>
              </author>
            </authors>
            <title>Flashbacks</title>
            <shippingWeight>1.4 pounds</shippingWeight>
            <isbn>978-0874778700</isbn>
          </important-data>
      

      主类

      package com.discursive.answers;
      
      import java.io.InputStream;
      
      import net.sf.json.JSON;
      import net.sf.json.xml.XMLSerializer;
      
      import org.apache.commons.io.IOUtils;
      
      public class ConvertXMLtoJSON {
      
              public static void main(String[] args) throws Exception {
                      InputStream is = 
                              ConvertXMLtoJSON.class.getResourceAsStream("sample-xml.xml");
                      String xml = IOUtils.toString(is);
      
                      XMLSerializer xmlSerializer = new XMLSerializer(); 
                      JSON json = xmlSerializer.read( xml );  
                      System.out.println( json.toString(2) );
              }
      }
      

      结果

      {
        "@certified": "true",
        "@processed": "true",
        "timestamp": "232423423423",
        "authors": [  {
          "firstName": "Tim",
          "lastName": "Leary"
        }],
        "title": "Flashbacks",
        "shippingWeight": "1.4 pounds",
        "isbn": "978-0874778700"
      }
      

      【讨论】:

      • 使用大型 xml 文件时出现 java 堆空间错误。感谢您的帮助
      【解决方案3】:

      结尾是这样的:

      public class BeeDetails
      {
          public string details { get; set; }
      
      }
      
      public class Bee
      {
          public BeeDetails { get; set; }
      }
      
      public class BeeResponse
      {
          public List<Bee> Bees { get; set; }
      }
      

      得到这个:

       {
        "bees": {
          "bee": { .. }, "bee": { .. }, ..
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多