简体   繁体   English

如何将 JavaObject 从 POJO 转换为 JSONString/JSONObject

[英]How to convert JavaObject from POJO to JSONString/JSONObject

I have a POJO which returns JAVAObject, Now I want to convert it to JSONObject but my POJO contains an array which is not converted using below code:我有一个返回 JAVAObject 的 POJO,现在我想将其转换为 JSONObject,但我的 POJO 包含一个未使用以下代码转换的数组:

Email Class: Email Class:

package pojo;

public class Email {

String TYPE;
String VALUE;

public Email() {
}

public Email(String TYPE, String VALUE) {

    this.TYPE = TYPE;

    this.VALUE = VALUE;

}

public void setTYPE(String TYPE) {

    this.TYPE = TYPE;

}

public String getTYPE() {

    return this.TYPE;

}

public void setVALUE(String VALUE) {

    this.VALUE = VALUE;

}

public String getVALUE() {

    return this.VALUE;
}

} }

PostAccountCreateAPI Class: PostAccountCreateAPI Class:

package pojo;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class PostAccountCreateAPI {

private String FirstName;
private String LastName;
private String PASSWORD;
private List<Email> Email;
static List<Email> emailList = new ArrayList<>();

public PostAccountCreateAPI() {
}


public void setFirstName(String FirstName) {

    this.FirstName = FirstName;

}

public String getFirstName() {

    return this.FirstName;

}

public void setLastName(String LastName) {

    this.LastName = LastName;

}

public String getLastName() {

    return this.LastName;

}

public void setPASSWORD(String PASSWORD) {

    this.PASSWORD = PASSWORD;

}

public String getPASSWORD() {

    return this.PASSWORD;

}

public void setEmail(List<Email> Email) {

    this.Email = Email;

}

public List<Email> getEmail() {

    return this.Email;

}

} }

I have created the Object of PostAccountCreateAPI Class and converting to JSONString as below:我创建了 PostAccountCreateAPI Class 的 Object 并转换为 JSONString,如下所示:

ObjectMapper mapper = new ObjectMapper();   
String json = mapper.writeValueAsString(postAccountCreateAPI);
System.out.println(json);

But I am not getting Email as array, Below is the response I got: {"lastName":null,"email":null,"firstName":null,"password":null} But I am not getting Email as array, Below is the response I got: {"lastName":null,"email":null,"firstName":null,"password":null}

I am expecting it in below format:我期待它采用以下格式:

    "FirstName": "FSFBE",
    "LastName": "LoUSj",
    "PASSWORD": "p@$$word123",
    "Email": [
        {
            "TYPE": "Primary",
            "VALUE": "test7EZK0@mail7.io"
        }
    ]
}

It looks like there is something wrong with your postAccountCreateAPI initialization.您的postAccountCreateAPI初始化似乎有问题。 Can you please copy the line, where you call the constructor and/or set the attributes?您能否复制调用构造函数和/或设置属性的行? Plus I think you should follow the java naming convention.另外,我认为您应该遵循 java 命名约定。 You violate almost every rule.你几乎违反了所有规则。 https://www.javatpoint.com/java-naming-conventions https://www.javatpoint.com/java-naming-conventions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM