用于表示 JSON 对象的通用 pojo
Generic pojo for representation of a JSON object
简单地说,我正在尝试编写一个代表 JSON 对象的 POJO,类似于:
{
"errorCode": "SYS101",
"errorMessage": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
}
因为每个键的名字is random unique and not in any pattern
和计数可能太多了,像下面这样写是没有用的:
public class Response {
private String errorCode;
private ErrorMessage errorMessage;
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public ErrorMessage getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(ErrorMessage errorMessage) {
this.errorMessage = errorMessage;
}
}
public class ErrorMessage {
private String key1;
private String key2;
private String key3;
public String getKey1() {
return key1;
}
public void setKey1(String key1) {
this.key1 = key1;
}
public String getKey2() {
return key2;
}
public void setKey2(String key2) {
this.key2 = key2;
}
public String getKey3() {
return key3;
}
public void setKey3(String key3) {
this.key3 = key3;
}
}
我想知道是否有通用的方法来编写表示 JSON 的 POJO?
更新:
键是随机字符串,它们不是任何数字模式:
比如你可以这样考虑:
{
"errorCode": "SYS101",
"errorMessage": {
"firstName": "first name could not be empty",
"age": "age could not be less thatn 18",
"gender": "could not be null"
}
}
也许是这样的?它不是很“通用”,但您可以根据需要添加任意数量的错误消息,并且它仍然保留原始 JSON 的结构,其中 errorMessage
是一个对象(而不是数组)
public class Response {
private String errorCode;
private Map<String, String> errorMessage; // JSONs are like maps with a key-value mapping
public Response(String errorCode, String... errorMessages){ // example for a constructor with the logic to assign the errorMessages (no setters)
this.errorCode = errorCode;
this.errorMessage= new HashMap<>(); // first you initialize the map
int count =0; // a counter to name your "keyX" keys
for(String msg:errorMessages){
errorMessage.put("key"+(++count),msg); // and filling the map
}
}
}
评论后更新
public class Response {
private String errorCode;
private Map<String, String> errorMessage; // you can still keep this map
public Response(String errorCode){ // I suppose the error messages can be added separately in setters
this.errorCode = errorCode;
this.errorMessage= new HashMap<>();
}
public void addErrorMessage(String key, String value){
if(this.errorMessage == null){
this.errorMessage= new HashMap<>(); // in case you don't want to initialize the map in your constructor
}
this.errorMessage.put(key, value); // there you go, just put any key value you like
}
}
此结构非常适合您的用例:
public class Response {
private String errorCode;
// This is useful when you do not know what the keys could be
private Map<String, String> errorMessage;
// ...setters/getters below
}
简单地说,我正在尝试编写一个代表 JSON 对象的 POJO,类似于:
{
"errorCode": "SYS101",
"errorMessage": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
}
因为每个键的名字is random unique and not in any pattern
和计数可能太多了,像下面这样写是没有用的:
public class Response {
private String errorCode;
private ErrorMessage errorMessage;
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public ErrorMessage getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(ErrorMessage errorMessage) {
this.errorMessage = errorMessage;
}
}
public class ErrorMessage {
private String key1;
private String key2;
private String key3;
public String getKey1() {
return key1;
}
public void setKey1(String key1) {
this.key1 = key1;
}
public String getKey2() {
return key2;
}
public void setKey2(String key2) {
this.key2 = key2;
}
public String getKey3() {
return key3;
}
public void setKey3(String key3) {
this.key3 = key3;
}
}
我想知道是否有通用的方法来编写表示 JSON 的 POJO?
更新: 键是随机字符串,它们不是任何数字模式:
比如你可以这样考虑:
{
"errorCode": "SYS101",
"errorMessage": {
"firstName": "first name could not be empty",
"age": "age could not be less thatn 18",
"gender": "could not be null"
}
}
也许是这样的?它不是很“通用”,但您可以根据需要添加任意数量的错误消息,并且它仍然保留原始 JSON 的结构,其中 errorMessage
是一个对象(而不是数组)
public class Response {
private String errorCode;
private Map<String, String> errorMessage; // JSONs are like maps with a key-value mapping
public Response(String errorCode, String... errorMessages){ // example for a constructor with the logic to assign the errorMessages (no setters)
this.errorCode = errorCode;
this.errorMessage= new HashMap<>(); // first you initialize the map
int count =0; // a counter to name your "keyX" keys
for(String msg:errorMessages){
errorMessage.put("key"+(++count),msg); // and filling the map
}
}
}
评论后更新
public class Response {
private String errorCode;
private Map<String, String> errorMessage; // you can still keep this map
public Response(String errorCode){ // I suppose the error messages can be added separately in setters
this.errorCode = errorCode;
this.errorMessage= new HashMap<>();
}
public void addErrorMessage(String key, String value){
if(this.errorMessage == null){
this.errorMessage= new HashMap<>(); // in case you don't want to initialize the map in your constructor
}
this.errorMessage.put(key, value); // there you go, just put any key value you like
}
}
此结构非常适合您的用例:
public class Response {
private String errorCode;
// This is useful when you do not know what the keys could be
private Map<String, String> errorMessage;
// ...setters/getters below
}