如何将以下JSON转换为Jackson java中的类
{
"status": 0,
"msg": "0 out of 1 Transactions Fetched Successfully",
"transaction_details": {
"3690": {
"mihpayid": "Not Found",
"status": "Not Found"
}
}
}
transaction_details中的整数键导致出现问题,我们无法控制从第三方支付网关返回的格式。
答案 0 :(得分:1)
如下所示,设计以下类,
import com.fasterxml.jackson.annotation.JsonProperty;
public class Jackson {
private String status;
private String msg;
private TransactionDetails transaction_details = new TransactionDetails();
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public TransactionDetails getTransaction_details() {
return transaction_details;
}
public void setTransaction_details(TransactionDetails transaction_details) {
this.transaction_details = transaction_details;
}
}
class TransactionDetails{
private A3690 a3690;
@JsonProperty("3690")
public A3690 getA3690() {
return a3690;
}
public void setA3690(A3690 a3690) {
this.a3690 = a3690;
}
}
class A3690{
private String mihpayid ;
private String status;
public String getMihpayid() {
return mihpayid;
}
public void setMihpayid(String mihpayid) {
this.mihpayid = mihpayid;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
你可以同样使用它来创建Jackson Object to JSON,
String jsonString = null;
try {
Jackson jacksonObj = new Jackson(); // populate it as per needs...
jsonString = objectMapper.writeValueAsString(jacksonObj);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(jsonString); // will return JSON into String formate.
将String JSON转换为Jackson Object也是如此,
String str = "{"status":"status","msg":"msg","transaction_details":{"3690":{"mihpayid":"mihpayid","status":"status1"}}}";
try {
Jackson jacksonObject = objectMapper.readValue(str, Jackson.class);
System.out.println(" Status -" + jacksonObject.getStatus() + " : msg - " + jacksonObject.getMsg());
System.out.println(" Mihpayid -" + jacksonObject.getTransaction_details().getA3690().getMihpayid() + " : Status - " + jacksonObject.getTransaction_details().getA3690().getStatus());
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
更新:
如果键3690未修复,
我的意思是它随机出现然后need to refactoring
杰克逊class likewise
,(这里只有杰克逊级足够所有,
不需要像我上面提到的那样创建更多的类)
private String status;
private String msg;
//private TransactionDetails transaction_details = new TransactionDetails();
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
HashMap<String, HashMap<String,String>> transaction_details = new HashMap<String, HashMap<String,String>>();
@JsonProperty("transaction_details")
public HashMap<String, HashMap<String,String>> getMap() {
return transaction_details;
}
public void setMap(HashMap<String, HashMap<String,String>> transaction_details) {
this.transaction_details = transaction_details;
}
}
享受,祝你好运...... !!!