Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用于表示JSON对象的通用pojo_Java_Json_Pojo - Fatal编程技术网

Java 用于表示JSON对象的通用pojo

Java 用于表示JSON对象的通用pojo,java,json,pojo,Java,Json,Pojo,简单地说,我正在尝试编写一个POJO,它表示一个JSON对象,如下所示: { "errorCode": "SYS101", "errorMessage": { "key1": "value1", "key2": "value2", "key3": "value3&q

简单地说,我正在尝试编写一个POJO,它表示一个JSON对象,如下所示:

{
    "errorCode": "SYS101",
    "errorMessage": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3"
    }   
}
由于每个键的名称
都是随机唯一的,并且没有任何模式
,而且计数可能太多,因此按以下方式书写是没有用的:

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
是一个对象(而不是数组)

公共类响应{
私有字符串错误码;
私有映射errorMessage;//JSON类似于具有键值映射的映射
公共响应(String errorCode、String…errorMessages){//具有分配errorMessages逻辑的构造函数示例(无设置器)
this.errorCode=错误代码;
this.errorMessage=newHashMap();//首先初始化映射
int count=0;//用于命名“keyX”键的计数器
对于(字符串消息:错误消息){
errorMessage.put(“key”+(++count),msg);//并填充映射
}          
}
}
评论后更新

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
    }
}

公共类响应{
私有字符串错误码;
private Map errorMessage;//您仍然可以保留此地图
公共响应(字符串错误代码){//我想可以在setters中单独添加错误消息
this.errorCode=错误代码;
this.errorMessage=newHashMap();
}
公共无效addErrorMessage(字符串键、字符串值){
if(this.errorMessage==null){
this.errorMessage=new HashMap();//以防您不想在构造函数中初始化映射
}
this.errorMessage.put(key,value);//好了,只需输入您喜欢的任何键值
}
}

此结构非常适合您的用例:

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

}
公共类响应{
私有字符串错误码;
//当您不知道钥匙可能是什么时,这非常有用
私有映射错误消息;
//…下面是setter/getter
}

如果
errorMessage
中的值是标量,请尝试
映射。如果嵌套对象可能更复杂,请尝试使用json库提供的泛型类,例如JSONObject和JSONArray。谢谢,但是键是随机字符串,它们不是任何数字模式,我更新了问题,使其更简单。我刚刚看到了这一点
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

}