10 分钟 jackjson 配置

// to enable standard indentation ("pretty-printing"):
mapper.enable(SerializationFeature.INDENT_OUTPUT);
// to allow serialization of "empty" POJOs (no properties to serialize)
// (without this setting, an exception is thrown in those cases)
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
// to write java.util.Date, Calendar as number (timestamp):
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

// DeserializationFeature for changing how JSON is read as POJOs:

// to prevent exception when encountering unknown property:
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
// to allow coercion of JSON empty String ("") to null Object value:
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

// to allow C/C++ style comments in JSON (non-standard, disabled by default)
mapper.enable(JsonParser.Feature.ALLOW_COMMENTS);
// to allow (non-standard) unquoted field names in JSON:
mapper.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
// to allow use of apostrophes (single quotes), non standard
mapper.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);

// JsonGenerator.Feature for configuring low-level JSON generation:

// to force escaping of non-ASCII characters:
mapper.enable(JsonGenerator.Feature.ESCAPE_NON_ASCII);


格式化输出


import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JackjsonCfg {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();

// to enable standard indentation ("pretty-printing"):
System.out.println("----------------------格式化输出-------------------");
mapper.enable(SerializationFeature.INDENT_OUTPUT);

Map<String, String> mapJson = new HashMap<String, String>();

mapJson.put("nameLove", "zhangs");
mapJson.put("nameLove1", "lis");

String sjson = mapper.writeValueAsString(mapJson);
System.out.println(sjson);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值