JSON中key為大寫解決方案
阿新 • • 發佈:2019-01-04
在對接第三方平臺時,拿到了一個json串,裡面的key為大寫,但自己定義的實體類屬性為小寫,轉換會出現問題
json如下:
{"data":{"ALTER":[{"alterDate":"2017-01-10","alterBefore":"200萬元","alterAfter":"10000萬元","alterItem":"註冊資本(金)變更"}]},"seqNum":"","message":"成功","status":0}
解決方案:
在實體類加入註解:@JsonProperty @JsonIgnore
- package com.jshx.uam.authReq.xml;
- import java.io.IOException;
- import net.sf.json.JSONObject;
- import org.codehaus.jackson.JsonGenerationException;
- import org.codehaus.jackson.annotate.JsonIgnore;
- import org.codehaus.jackson.annotate.JsonProperty;
- import org.codehaus.jackson.map.JsonMappingException;
- import org.codehaus.jackson.map.ObjectMapper;
- public class SessionHeader {
- @JsonProperty("ActionCode")
- private String ActionCode = "";
- @JsonProperty("TransactionID")
- private String TransactionID = "";
- @JsonProperty("RspTime")
- private String RspTime = "";
- @JsonProperty("DigitalSign")
- private String DigitalSign = "";
- @JsonProperty("Response")
- private Response Response;
- public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
- SessionHeader sh = new SessionHeader();
- sh.setActionCode("1");
- ObjectMapper mapper = new ObjectMapper();
- String json=mapper.writeValueAsString(sh);
- System.out.println(json);
- sh=mapper.readValue(json, SessionHeader.class);
- System.out.println(sh.getActionCode());
- }
- @JsonIgnore
- public String getActionCode() {
- return ActionCode;
- }
- @JsonIgnore
- public void setActionCode(String actionCode) {
- ActionCode = actionCode;
- }
- @JsonIgnore
- public String getTransactionID() {
- return TransactionID;
- }
- @JsonIgnore
- public void setTransactionID(String transactionID) {
- TransactionID = transactionID;
- }
- @JsonIgnore
- public String getRspTime() {
- return RspTime;
- }
- @JsonIgnore
- public void setRspTime(String rspTime) {
- RspTime = rspTime;
- }
- @JsonIgnore
- public String getDigitalSign() {
- return DigitalSign;
- }
- @JsonIgnore
- public void setDigitalSign(String digitalSign) {
- DigitalSign = digitalSign;
- }
- @JsonIgnore
- public Response getResponse() {
- return Response;
- }
- @JsonIgnore
- public void setResponse(Response response) {
- Response = response;
- }
- }