英文:
Including nested objects in the JSON of a Spring Boot application
问题
以下是已翻译的内容:
{
"STATUS": "SUCCESS",
"DATA": {
"ACTIVATION": {
"aid": "0",
"app": "OASIS_EDGE",
"actkey": "STAR2020",
"expiredate": "2025-08-05 04:40:28.0",
"partner": "X",
"nsp": "NAD"
}
}
}
英文:
Can anyone guide me on how to nest the JSON object Activation under the JSON object Data.
See my JSON below:
{
"status": "Success",
"DATA": {
"aid": "0",
"app": "OASIS_EDGE",
"actkey": "STAR2020",
"expiredate": "2025-08-05 04:40:28.0",
"partner": "X",
"nsp": "NAD"
}
}
The JSON I actually expect:
{
"STATUS": "SUCCESS",
"DATA": {
"ACTIVATION": {
"aid": "0",
"app": "OASIS_EDGE",
"actkey": "STAR2020",
"expiredate": "2025-08-05 04:40:28.0",
"partner": "X",
"nsp": "NAD"
}
}
}
Here is my ActivationResponse.java class responsible for creating the JSON:
public class ActivationResponse {
private String Status;
public String getStatus()
{
return Status;
}
public void setStatus(String Status) {
this.Status = Status;
}
@JsonProperty("DATA")
private ActivationModel activationData;
public ActivationModel getActivationData() {
return activationData;
}
public void setActivationData(ActivationModel activationData2) {
this.activationData = activationData2;
}
}
答案1
得分: 1
因为您希望在数据下进行激活,所以需要一个带有激活字段的数据类,就像下面的示例一样。
class MyJson {
private String Status;
@JsonProperty("DATA")
private DataModel data;
};
class DataModel {
@JsonProperty("ACTIVATION")
private ActivationModel activation;
};
英文:
Since you want activation under data, you need a class data with a field for activation, like the example below.
class MyJson {
private String Status;
@JsonProperty("DATA")
private DataModel data;
};
class DataModel {
@JsonProperty("ACTIVATION")
private ActivationModel activation;
};
答案2
得分: 0
- 使用 JsonNode 来自 com.fasterxml.jackson.databind.JsonNode 的方式:
import com.fasterxml.jackson.databind.JsonNode;
public class ActivationResponse {
private String Status;
private JsonNode activationData;
public String getStatus() {
return Status;
}
public void setStatus(String Status) {
this.Status = Status;
}
public JsonNode getActivationData() {
return activationData;
}
public void setActivationData(JsonNode activationData2) {
activationData = activationData2;
}
}
- 使用来自 java.util.Map 的经典 Map,如下所示:
import java.util.Map;
public class ActivationResponse {
private String Status;
private Map<String, Object> activationData;
public String getStatus() {
return Status;
}
public void setStatus(String Status) {
this.Status = Status;
}
public Map<String, Object> getActivationData() {
return activationData;
}
public void setActivationData(Map<String, Object> activationData2) {
activationData = activationData2;
}
}
我毫不羞怯地借用了这个教程(没有关联)!
英文:
- Use JsonNode from com.fasterxml.jackson.databind.JsonNode à la:
import com.fasterxml.jackson.databind.JsonNode;
public class ActivationResponse {
private String Status;
private JsonNode activationData;
public String getStatus() return Status;
public void setStatus(String Status) this.Status = Status;
public ActivationModel getActivationData() return activationData;
public void setActivationData(ActivationModel activationData2) {
activationData = activationData2;
}
}
- Use the good 'ol Map from java.util.Map like so:
import java.util.Map;
public class ActivationResponse {
private String Status;
private Map<String, Object> activationData;
public String getStatus() return Status;
public void setStatus(String Status) this.Status = Status;
public ActivationModel getActivationData() return activationData;
public void setActivationData(ActivationModel activationData2) {
activationData = activationData2;
}
}
I shamelessly borrowed from this tutorial (no affiliations)!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论