@JsonProperty和@JSONField

123 阅读1分钟

fastjson1其实是不支持JsonProperty的,用了也不生效。

使用fastjson1的JSONObject.parseObject默认可以把下划线(json)转成驼峰(java 对象),但如果修改属性名称,就得使用JSONField了。 而fastjson1的JSONObject.toJSONString默认无法把驼峰转为下划线,如果想转的话,也得使用JSONField。

fastjson2其实是支持JsonProperty。

测试代码

@Data
    public static class WeekendCaseStateRsp {
//        @JsonProperty("task_id")
        private long taskId;

        @JsonProperty("plan_id")
        private long planId;

        @JsonProperty("case_id")
        private long caseId;

        @JsonProperty("case_state")
        private int caseState;

        @JSONField(name="tester")
//        @JSONField(name="tester")
        private String finalExecutor;

//        @JsonProperty("tester")
//        private String tester;
    }

String str1 = "{"tester":"heyalu","case_id":"123456"}";
WeekendCaseStateClient.WeekendCaseStateRsp weekendCaseStateRsp = JSONObject.parseObject(str1, WeekendCaseStateClient.WeekendCaseStateRsp.class);
String str2 = JSONObject.toJSONString(weekendCaseStateRsp);