在fastjson-1.2.12版本中,JSONField支持一個新的配置項jsonDirect,它的用途是:當你有一個字段是字符串類型,裏面是json格式數據,你希望直接輸入,而不是經過轉義之後再輸出。
Model
import com.alibaba.fastjson.annotation.JSONField; public static class Model { public int id; @JSONField(jsonDirect=true) public String value; }
Usage
Model model = new Model(); model.id = 1001; model.value = "{}"; String json = JSON.toJSONString(model); Assert.assertEquals("{\"id\":1001,\"value\":{}}", json);
原文:https://github.com/alibaba/fastjson/wiki/JSONField_jsonDirect_cn