gpt4 book ai didi

java - 需要帮助构建 JSONObject java

转载 作者:搜寻专家 更新时间:2023-11-01 03:05:58 26 4
gpt4 key购买 nike

我正在尝试在 java 中动态构建以下 json 对象,例如,如果 WARN 对象不存在,请添加它或任何其他对象,然后将新的标签消息对象添加到子数组。

这是我正在尝试动态构建的示例。

{
"warnings" : [
{
"WARN" : [
{
"label" : "Label Goes Here",
"message" : "Message Goes Here"
},
{
"label" : "Label Goes Here2",
"message" : "Message Goes Here2"
}
],"title" : "Please review the following warnings"
},
{
"NOTIFICATION" : [
{
"label" : "Label Goes Here3",
"message" : "Message Goes Here3"
},
{
"label" : "Label Goes Here4",
"message" : "Message Goes Here4"
}
],"title" : "Please review the following warnings"
}
]
}

这是我试过的。

public class Warning {
warningTypes = new JSONObject();
}

private JSONObject warningTypes;

public Warning() {

public Warning(WarningType warningType, String label, String message) {
this.warningType = warningType;
this.label = label;
this.message = message;
}

public void add(WarningType warningType, String label, String message) {
addToJSON(warningType, new JSONObject("label",label,"message",message));
}

private void addToJSON(WarningType warningType, JSONObject jsonObj) {
if(warningTypes.has(warningType.name())) {
JSONArray array = warningTypes.getJSONArray(warningType.name());
array.put(jsonObj);
} else {

warningTypes.put(warningType.name(), new JSONArray(jsonObj));
}
}

public JSONObject toJSON() {
return new JSONObject("warnings", new JSONArray(warningTypes));
}

}

然而,这是我的结果,您可以看到它是不正确的。我无法将标题 do 添加到我的 warningTypes 的事实中,而是添加到单个对象中。

{
"warnings" : [
{
"WARN" : [
{
"label" : "Label Goes Here",
"message" : "Message Goes Here"
},
{
"label" : "Label Goes Here2",
"message" : "Message Goes Here2"
}
],
"NOTIFICATION" : [
{
"label" : "Label Goes Here3",
"message" : "Message Goes Here3"
},
{
"label" : "Label Goes Here4",
"message" : "Message Goes Here4"
}
]
}
]
}

我不知道如何动态构建这个对象,任何帮助将不胜感激。

最佳答案

您尝试创建的 JSON 没有相同的键。以下代码将为您提供所需的输出。根据需要将部件重构为方法。

代码:

public static class Message {
private String label;
private String message;

public String getMessage() {
return message;
}

public String getLabel() {
return label;
}

public void setMessage(String message) {
this.message = message;
}

public void setLabel(String label) {
this.label = label;
}
}

public static enum WarningType {
WARN, NOTIFICATION
}

public static class Warning {

WarningType type;
List<Message> messages;
String title;

public WarningType getType() {
return type;
}

public void setType(WarningType type) {
this.type = type;
}

public void setMessages(List<Message> messages) {
this.messages = messages;
}

public void setTitle(String title) {
this.title = title;
}

public List<Message> getMessages() {
return messages;
}

public String getTitle() {
return title;
}
}

public static class Warnings {
List<Map<String, Object>> warnings;

public List<Map<String, Object>> getWarnings() {
return warnings;
}

public void setWarnings(List<Map<String, Object>> warnings) {
this.warnings = warnings;
}

public void setWarningsInMap(List<Warning> warningList) {
warnings = new ArrayList<>();
for(Warning each : warningList) {
Map<String, Object> m = new LinkedHashMap<>();
m.put(each.getType().name(), each.getMessages());
m.put("title", each.getTitle());
warnings.add(m);
}
}
}

public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
List<Warning> warningList = new ArrayList<>();

Warning warn = new Warning();
warn.setType(WarningType.WARN);

List<Message> warnMessages = new ArrayList<>();

Message m = new Message();
m.setLabel("Label Goes Here");
m.setMessage("Message Goes Here");
warnMessages.add(m);

m = new Message();
m.setLabel("Label Goes Here2");
m.setMessage("Message Goes Here2");
warnMessages.add(m);

warn.setMessages(warnMessages);

warn.setTitle("Please review the following warnings");

warningList.add(warn);

Warning notification = new Warning();
notification.setType(WarningType.NOTIFICATION);

List<Message> notificationMessages = new ArrayList<>();

m = new Message();
m.setLabel("Label Goes Here3");
m.setMessage("Message Goes Here3");
notificationMessages.add(m);

m = new Message();
m.setLabel("Label Goes Here4");
m.setMessage("Message Goes Here4");
notificationMessages.add(m);

notification.setMessages(notificationMessages);

notification.setTitle("Please review the following warnings");

warningList.add(notification);

Warnings w = new Warnings();
w.setWarningsInMap(warningList);

String s = new ObjectMapper().defaultPrettyPrintingWriter().writeValueAsString(w);
System.out.println(s);
}

输出:

{
"warnings" : [ {
"WARN" : [ {
"message" : "Message Goes Here",
"label" : "Label Goes Here"
}, {
"message" : "Message Goes Here2",
"label" : "Label Goes Here2"
} ],
"title" : "Please review the following warnings"
}, {
"NOTIFICATION" : [ {
"message" : "Message Goes Here3",
"label" : "Label Goes Here3"
}, {
"message" : "Message Goes Here4",
"label" : "Label Goes Here4"
} ],
"title" : "Please review the following warnings"
} ]
}

关于java - 需要帮助构建 JSONObject java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22233789/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com