Java格式化打印json,如何格式化Json输出

Please help me how to get JSON output as below:

{

"_costMethod": "Average",

"fundingDate": 2008-10-02,

"fundingAmount": 2510959.95

}

Instead of:

{

"@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 Model

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.

The @XmlElement annotation can be used to change the name of a key.

I have used the @XmlSchemaType annotation to control the format of the Date 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

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

Demo Code

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 properties = new HashMap(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);

}

}

Output

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

}

For More Information

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值