alibaba / fastjson

FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
https://github.com/alibaba/fastjson2/wiki/fastjson_1_upgrade_cn
Apache License 2.0
25.76k stars 6.5k forks source link

Springmvc 自动匹配json时key 有大写字母的问题 #890

Open Sligths opened 8 years ago

Sligths commented 8 years ago

如下的样例: { "ActionType" : "ADD", "TransactionID" : "9790b44b-d008-4b4f-a27c-4cf4b167859f42342", "DataType" : "SGMHRMSEMPL", "Hrms_Empl" : { "EMPL_FLAG" : "在线", "EE_TYPE" : "1", "INFO":[{"DATE":"2016-07-26 10:41:00"}] } 请设计一个可以匹配的bean,可以获取里面的内容,特别是key为"INFO"的内容 谢谢

ljluestc commented 1 month ago

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.List;

// Main class to test parsing
public class Main {
    public static void main(String[] args) {
        String jsonStr = "{ \"ActionType\" : \"ADD\", \"TransactionID\" : \"9790b44b-d008-4b4f-a27c-4cf4b167859f42342\", \"DataType\" : \"SGMHRMSEMPL\", \"Hrms_Empl\" : { \"EMPL_FLAG\" : \"在线\", \"EE_TYPE\" : \"1\", \"INFO\": [{\"DATE\": \"2016-07-26 10:41:00\"}] } }";

        // 解析JSON字符串为对应的类
        Root root = JSON.parseObject(jsonStr, Root.class);

        // 获取并打印INFO字段的内容
        List<Info> infoList = root.getHrmsEmpl().getInfo();
        for (Info info : infoList) {
            System.out.println("DATE: " + info.getDate());
        }

        // 输出整个对象的内容
        System.out.println(root);
    }
}

// Root class to map the outer JSON object
class Root {
    private String ActionType;
    private String TransactionID;
    private String DataType;
    private HrmsEmpl Hrms_Empl;

    // Getters and Setters
    public String getActionType() {
        return ActionType;
    }

    public void setActionType(String actionType) {
        ActionType = actionType;
    }

    public String getTransactionID() {
        return TransactionID;
    }

    public void setTransactionID(String transactionID) {
        TransactionID = transactionID;
    }

    public String getDataType() {
        return DataType;
    }

    public void setDataType(String dataType) {
        DataType = dataType;
    }

    public HrmsEmpl getHrmsEmpl() {
        return Hrms_Empl;
    }

    public void setHrmsEmpl(HrmsEmpl hrmsEmpl) {
        Hrms_Empl = hrmsEmpl;
    }

    @Override
    public String toString() {
        return "Root{" +
                "ActionType='" + ActionType + '\'' +
                ", TransactionID='" + TransactionID + '\'' +
                ", DataType='" + DataType + '\'' +
                ", Hrms_Empl=" + Hrms_Empl +
                '}';
    }
}

// Class representing the "Hrms_Empl" object
class HrmsEmpl {
    private String EMPL_FLAG;
    private String EE_TYPE;
    private List<Info> INFO;

    // Getters and Setters
    public String getEMPL_FLAG() {
        return EMPL_FLAG;
    }

    public void setEMPL_FLAG(String EMPL_FLAG) {
        this.EMPL_FLAG = EMPL_FLAG;
    }

    public String getEE_TYPE() {
        return EE_TYPE;
    }

    public void setEE_TYPE(String EE_TYPE) {
        this.EE_TYPE = EE_TYPE;
    }

    public List<Info> getInfo() {
        return INFO;
    }

    public void setInfo(List<Info> INFO) {
        this.INFO = INFO;
    }

    @Override
    public String toString() {
        return "HrmsEmpl{" +
                "EMPL_FLAG='" + EMPL_FLAG + '\'' +
                ", EE_TYPE='" + EE_TYPE + '\'' +
                ", INFO=" + INFO +
                '}';
    }
}

// Class representing the "INFO" array's objects
class Info {
    private String DATE;

    // Getters and Setters
    public String getDate() {
        return DATE;
    }

    public void setDate(String DATE) {
        this.DATE = DATE;
    }

    @Override
    public String toString() {
        return "Info{" +
                "DATE='" + DATE + '\'' +
                '}';
    }
}