包括嵌套对象在Spring Boot应用程序的JSON中

huangapple go评论67阅读模式
英文:

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

  1. 使用 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;
    }
}
  1. 使用来自 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;
    }
}

我毫不羞怯地借用了这个教程(没有关联)!

英文:
  1. 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;
    }
}
  1. Use the good 'ol Map from java.util.Map like so:
import java.util.Map;

public class ActivationResponse {
    
    private String Status;

    private Map&lt;String, Object&gt; 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)!

huangapple
  • 本文由 发表于 2020年8月8日 09:49:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63311070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定