Java XML包装器JSON格式问题
我有这门课:Java XML包装器JSON格式问题,java,xml,json,xml-parsing,Java,Xml,Json,Xml Parsing,我有这门课: class A { @XmlElement(name = "bees") @XmlElementWrapper(name="bee") public List<B> bees; } 我需要: { "bees": { "bee": { .. }, "bee": { .. }, .. } } 有人能帮忙吗?用这个;我遇到了你所描述的同样的问题。我们使用的解决方案是将Maven依赖项从Jackson更改为j
class A {
@XmlElement(name = "bees")
@XmlElementWrapper(name="bee")
public List<B> bees;
}
我需要:
{
"bees": {
"bee": { .. }, "bee": { .. }, ..
}
}
有人能帮忙吗?用这个;我遇到了你所描述的同样的问题。我们使用的解决方案是将Maven依赖项从Jackson更改为json库。我们将此网站用作:
如果使用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>
结果
{
"@certified": "true",
"@processed": "true",
"timestamp": "232423423423",
"authors": [ {
"firstName": "Tim",
"lastName": "Leary"
}],
"title": "Flashbacks",
"shippingWeight": "1.4 pounds",
"isbn": "978-0874778700"
}
用这个,;我遇到了你所描述的同样的问题。我们使用的解决方案是将Maven依赖项从Jackson更改为json库。我们将此网站用作:
如果使用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>
结果
{
"@certified": "true",
"@processed": "true",
"timestamp": "232423423423",
"authors": [ {
"firstName": "Tim",
"lastName": "Leary"
}],
"title": "Flashbacks",
"shippingWeight": "1.4 pounds",
"isbn": "978-0874778700"
}
注:我是专家组的负责人和成员
而不是:
{
"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);
}
}
更多信息
注:我是专家组的负责人和成员
而不是:
{
"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);
}
}
更多信息
以如下方式结束:
<a>
<bees>
<bee>...</bee>
<bee>...</bee>
</bees>
</a>
public class BeeDetails
{
public string details { get; set; }
}
public class Bee
{
public BeeDetails { get; set; }
}
public class BeeResponse
{
public List<Bee> Bees { get; set; }
}
以如下方式结束:
<a>
<bees>
<bee>...</bee>
<bee>...</bee>
</bees>
</a>
public class BeeDetails
{
public string details { get; set; }
}
public class Bee
{
public BeeDetails { get; set; }
}
public class BeeResponse
{
public List<Bee> Bees { get; set; }
}
使用大型xml文件时出现java堆空间错误。感谢您的帮助,因为使用大型xml文件时出现Java堆空间错误。谢谢你的帮助