JSONObject.toJSONString首字母大小寫問題的三種解決方案

1、在字段上加註解@JSONField

package com.cjh.wechatmp.po;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Data
public class InMessageText {

    @JSONField(name = "ToUserName")
    private String ToUserName;
    @JSONField(name = "FromUserName")
    private String FromUserName;
    private String CreateTime;
    private String MsgType;
    private String Content;
    private String MsgId;
}

優點:少量的話簡單方便

缺點:入侵性強,懶得每個加

2、自定義配置

優點: 逼格高

缺點:並不是忽略不轉,而是轉了小寫,再轉回來大寫

package com.wechat.util;

import java.util.Map;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.JavaIdentifierTransformer;
import net.sf.json.util.PropertySetStrategy;

public class JsonUtil {

    public static Object toBean(JSONObject jsonObject, Class rootClass,
        String[] ignoreLowercase, Map<String, Class> childClass) {
        JsonConfig config = new JsonConfig();

        //轉換對象
        config.setRootClass(rootClass);

        //首字母小寫
        config.setJavaIdentifierTransformer(new JavaIdentifierTransformer() {
            @Override
            public String transformToJavaIdentifier(String str) {
                if (ignoreLowercase.length > 0) {
                    for (String item : ignoreLowercase) {
                        if (str.indexOf(item) == 0) {
                            return str;
                        }
                    }
                }
                char[] chars = str.toCharArray();
                chars[0] = Character.toLowerCase(chars[0]);
                return new String(chars);
            }
        });

        //對象裏沒有的屬性忽略
        config.setPropertySetStrategy(new PropertySetStrategy() {
            @Override
            public void setProperty(Object o, String s, Object o1) throws JSONException {
                try {
                    PropertySetStrategy.DEFAULT.setProperty(o, s, o1);
                } catch (JSONException e) {
                }
            }
        });

        //自定義的list聲明
        if (childClass != null) {
            config.setClassMap(childClass);
        }

        return JSONObject.toBean(jsonObject, config);
    }

}

3、用Gson代替(推薦)

優點:簡單方便

缺點:要添加依賴,不過不會和之前的衝突哦

<!-- gson -->
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.5</version>
    </dependency>

效果:

InMessageText(ToUserName=toUser, FromUserName=fromUser, CreateTime=1348831860, MsgType=text, Content=this is a test, MsgId=1234567890123456)
<xml><Content><![CDATA[this is a test]]></Content><CreateTime><![CDATA[1348831860]]></CreateTime><ToUserName><![CDATA[toUser]]></ToUserName><FromUserName><![CDATA[fromUser]]></FromUserName><MsgType><![CDATA[text]]></MsgType><MsgId><![CDATA[1234567890123456]]></MsgId></xml>
Gson--->{"ToUserName":"toUser","FromUserName":"fromUser","CreateTime":"1348831860","MsgType":"text","Content":"this is a test","MsgId":"1234567890123456"}
JSONObject--->{"content":"this is a test","createTime":"1348831860","fromUserName":"fromUser","msgId":"1234567890123456","msgType":"text","toUserName":"toUser"}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章