当前位置: 首页 > 面试题库 >

使用Marshaller将Java对象转换为Json

卢毅
2023-03-14
问题内容

使用Marshaller将Java对象转换为XML相当容易。但是我需要单独使用marshaller将Java对象转换为JSON。我知道像使用gson或Xstream这样的东西很好,但是我需要使用Marshaller。如何实现呢?

提前致谢。


问题答案:

注意: 我是 EclipseLink
JAXB(MOXy)的
负责人,并且是
JAXB(JSR-222)
专家组的成员,

如果将MOXy用作JAXB提供程序,以下是如何完成的。

  • http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html

JAVA模型

顾客

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement(namespace="http://www.example.com")
@XmlType(namespace="http://www.example.com")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {

    @XmlAttribute
    private int id;

    @XmlElement(namespace="http://www.example.com")
    private String firstName;

    @XmlElement(namespace="http://www.example.com", nillable=true)
    private String lastName;

    @XmlElement(namespace="http://www.example.com")
    private List<PhoneNumber> phoneNumbers = new ArrayList<PhoneNumber>();

}

电话号码

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class PhoneNumber {

    @XmlAttribute
    private String type;

    @XmlValue
    private String number;

}

jaxb.properties

要将MOXy指定为JAXB提供程序,您需要jaxb.properties在与域模型相同的程序包中包含一个名为的文件,并包含以下条目(请参阅:http : //blog.bdoughan.com/2011/05/specifying-
eclipselink-moxy-as -your.html

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

演示代码

input.xml

<?xml version="1.0" encoding="UTF-8"?>
<ns0:customer xmlns:ns0="http://www.example.com" id="123">
   <ns0:firstName>Jane</ns0:firstName>
   <ns0:lastName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
   <ns0:phoneNumbers type="work">555-1111</ns0:phoneNumbers>
</ns0:customer>

演示版

在下面的演示代码中,我们将使用相同的JAXB元数据将XML文档转换为Java对象,然后将这些对象转换回JSON。使用MOXy,您可以通过在上设置属性来指定JSON输出Marshaller

import java.io.File;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.MarshallerProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Customer.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum15357366/input.xml");
        Customer customer = (Customer) unmarshaller.unmarshal(xml)
                ;
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
        marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
        marshaller.marshal(customer, System.out);
    }

}

JSON输出

以下是JSON输出。注意,没有名称空间或XML属性对应的指示符。还要注意,大小为1的集合已正确表示为JSON数组(其他方法存在的问题)。

{
   "id" : 123,
   "firstName" : "Jane",
   "lastName" : null,
   "phoneNumbers" : [ {
      "type" : "work",
      "value" : "555-1111"
   } ]
}


 类似资料:
  • 问题内容: 我希望我的JSON看起来像这样: 到目前为止的代码: 和 我只是缺少如何使用Jackson将Java对象转换为JSON的部分: 我的问题是:我的课程正确吗?我必须调用哪个实例,以及如何实现此JSON输出? 问题答案: 要使用Jackson 转换JSON:

  • 我希望我的JSON如下所示: 迄今为止的代码: 和 我只是错过了如何使用Jackson将Java对象转换为JSON的部分: 我的问题是:我的课正确吗?我必须调用哪个实例,以及如何实现这个JSON输出?

  • 问题内容: 我正在将struts2用于Action,将jquery用于UI … 我想知道如何将Map对象转换为JSON对象并将其发送回UI, 现在可以在JSP页面中将其打印为普通的Java Map对象: 但我希望它是这样的: 我将如何实现这一目标…? 问题答案: 尝试Gson: 不过,我不建议将这种代码放入JSP。诸如此类的事情应该存在于Servlet或Action类之类的控制器中。 您还绝对不希

  • 问题内容: 我对服务进行了调用,并将响应存储在中。但是,我试图将其转换为类对象并得到错误。这是我的代码: 响应如下所示: 这是该类的样子: 但是我得到了错误: 我究竟做错了什么? 编辑1: 这是从答案中使用gson的尝试: 但是我得到了错误: 问题答案: 找出问题所在。需要提取jsonobject而不是获取字符串。这是解决此问题的行:

  • 早上好 相对来说,我是java新手,正在尝试完成一个项目,但是我遇到了问题。我的程序使用SQL构建一个URL来调用web服务,该服务将返回以下JSON: 我需要做的是在末尾去掉“False”或“True”值,根据其结果执行其他逻辑。 我不断得到以下错误: 这是我在WebCall中构建URL后的代码。java类: 在我的第二节课中,代码如下: 关于可能出错的任何想法?我觉得这可能与JSON响应上的[

  • 例如:Date值为:“dateCollected”:fri Jul 07 00:00:00 IST 1989, 但它只服用星期五而不是整个日期。