如何格式化Json输出 [英] How to format Json output
问题描述
请帮助我如何获取JSON输出,如下所示:
Please help me how to get JSON output as below:
{
"_costMethod": "Average",
"fundingDate": 2008-10-02,
"fundingAmount": 2510959.95
}
代替:
{
"@type": "sma",
"costMethod": "Average",
"fundingDate": "2008-10-02",
"fundingAmount": "2510959.95"
}
推荐答案
Based on the output from your question, you are currently not using EclipseLink JAXB (MOXy)'s native JSON binding. The following should help.
Java模型
以下是我根据您的帖子对您的对象模型的最佳猜测.我已经添加了获取所需的输出所必需的元数据.
Below is my best guess at your object model based on your post. I have added the metadata necessary to get the output that you are looking for.
-
@XmlElement
批注可用于更改键的名称. - 我已经使用
@XmlSchemaType
批注来控制Date
属性的格式.
- The
@XmlElement
annotation can be used to change the name of a key. - I have used the
@XmlSchemaType
annotation to control the format of theDate
property.
package forum14047050;
import java.util.Date;
import javax.xml.bind.annotation.*;
@XmlType(propOrder={"costMethod", "fundingDate", "fundingAmount"})
public class Sma {
private String costMethod;
private Date fundingDate;
private double fundingAmount;
@XmlElement(name="_costMethod")
public String getCostMethod() {
return costMethod;
}
public void setCostMethod(String costMethod) {
this.costMethod = costMethod;
}
@XmlSchemaType(name="date")
public Date getFundingDate() {
return fundingDate;
}
public void setFundingDate(Date fundingDate) {
this.fundingDate = fundingDate;
}
public double getFundingAmount() {
return fundingAmount;
}
public void setFundingAmount(double fundingAmount) {
this.fundingAmount = fundingAmount;
}
}
jaxb.properties
要将MOXy用作JAXB(JSR-222)提供程序,您需要在与域模型相同的程序包中包含一个名为jaxb.properties
的文件(请参阅:
To use MOXy as your JAXB (JSR-222) provider you need to include a file called jaxb.properties
in the same package as your domain model (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
演示代码
package forum14047050;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(2);
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
JAXBContext jc = JAXBContext.newInstance(new Class[] {Sma.class}, properties);
Sma sma = new Sma();
sma.setCostMethod("Average");
sma.setFundingDate(new Date(108, 9, 2));
sma.setFundingAmount(2510959.95);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(sma, System.out);
}
}
输出
下面是运行演示代码的输出.与您的问题不同,我在日期值周围加了引号.这是使其成为有效JSON所必需的.
Below is the output from running the demo code. Unlike your question I have quotes around the date value. This is required to make it valid JSON.
{
"_costMethod" : "Average",
"fundingDate" : "2008-10-02",
"fundingAmount" : 2510959.95
}
更多信息
- http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html
- http://blog. bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html
- http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html
- http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html
这篇关于如何格式化Json输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!