保留空值–;Java POJO到org.JSONObject

保留空值–;Java POJO到org.JSONObject,java,json,spring-boot,serialization,org.json,Java,Json,Spring Boot,Serialization,Org.json,我正在使用SpringBoot在Java中创建一个RESTfulAPI服务。我有一个试图从中创建JSON对象的类。一切都很好——除了类实例中的json对象忽略了空值之外。我希望保留键并将值设置为null,而不是将它们从输出中完全剥离 输出如下(为简洁起见缩短) 优先输出 以下是JsonQuestionObject: 以下是JSON创建者方法(它接受JSONQuestionObject数组) publicjsonobjectcreatequestionsjson(数组列表问题){ JSONObje

我正在使用SpringBoot在Java中创建一个RESTfulAPI服务。我有一个试图从中创建JSON对象的类。一切都很好——除了类实例中的json对象忽略了空值之外。我希望保留键并将值设置为null,而不是将它们从输出中完全剥离

输出如下(为简洁起见缩短) 优先输出 以下是JsonQuestionObject: 以下是JSON创建者方法(它接受JSONQuestionObject数组)
publicjsonobjectcreatequestionsjson(数组列表问题){
JSONObject输出=新的JSONObject();
JSONArray content=新的JSONArray();
JSONObject subcent=新的JSONObject();
JSONArray qarray=新的JSONArray();
对于(int i=0;i4&&qnum<7){
qarray.put(qitem);
}否则如果(qnum>6&&qnum<9){
qarray.put(qitem);
}否则如果(qnum>8&&){
qarray.put(qitem);
}
}
分包。提出(“问题”,qarray);
内容。提交(分包);
输出。put(“输出”,内容);
返回输出;
}
除了通过使用每个属性的setter和getter以及设置
JSONObject.put(“answer”,JSONObject.null)
手动创建JSONObject之外,还有其他方法保持这些空值吗


非常感谢您的帮助。

请尝试将
spring.jackson.default property inclusion=始终
添加到您的
应用程序。属性


另外,查看
spring.jackson
系列中的其他设置,了解JSON序列化的其他设置。

使用构造函数
公共JSONObject(对象bean)创建新的
JSONObject
时,内部实现会跳过所有
null
值。要保持
null
-s,您必须自己创建并填充新的
JSONObject

另一方面,您不需要使用
org.json.*
library。您可以使用
Map
s和
List
s构建类似的结构。见以下结构:

JsonQuestionObject[] questionArray = {new JsonQuestionObject()};
HashMap<String, Object> questions = new HashMap<>();
questions.put("questions", questionArray);

List<Object> questionsList = new ArrayList<>();
questionsList.add(questions);

Map<String, Object> result = Collections.singletonMap("output", questionsList);

我试图添加它,但它没有任何作用。是否需要添加任何额外的依赖项才能使其正常工作?似乎我根本没有使用jackson,因为我使用的是org.json依赖项?@user2868900,对不起,我从来没有使用过
org.json
库-我使用
jackson
。我唯一能建议的是寻找序列化程序设置——我想,它应该有类似的参数。
    "questions": [
        {
            "question": "question1",
            "svg": "svg1",
            "questionNum": 1, 
            "shuffledNum": null, 
            "answer": null

        },
        {
            "question": "question2"
            "svg": "q2",
            "questionNum": 2,
            "shuffledNum": null,
            "answer": null 
        }
    ],
public class JsonQuestionObject {

    private Integer questionNum;
    private String svg;
    private Integer shuffledNum = null;
    private String question;
    private Integer answer = null;

    //Getters & Setters
}
public JSONObject createQuestionsJson(ArrayList<JsonQuestionObject> questions) {
    JSONObject output = new JSONObject();
    JSONArray content = new JSONArray();
    JSONObject subContent = new JSONObject();
    JSONArray qarray = new JSONArray();


    for (int i = 0; i < questions.size(); i++) {
        JsonQuestionObject q = questions.get(i);
        Integer qnum = q.getQuestionNum();
        JSONObject qitem = new JSONObject(q);

        if (qnum < 5) {
            qarray.put(qitem);
        } else if (qnum > 4 && qnum < 7) {
            qarray.put(qitem);
        } else if (qnum > 6 && qnum < 9 ) {
            qarray.put(qitem);
        } else if (qnum > 8 &&) {
            qarray.put(qitem);
        }
    }
    subContent.put("questions", qarray);
    content.put(subContent);
    output.put("output", content);

    return output;
}
JsonQuestionObject[] questionArray = {new JsonQuestionObject()};
HashMap<String, Object> questions = new HashMap<>();
questions.put("questions", questionArray);

List<Object> questionsList = new ArrayList<>();
questionsList.add(questions);

Map<String, Object> result = Collections.singletonMap("output", questionsList);
@JsonInclude(JsonInclude.Include.ALWAYS)
public class JsonQuestionObject