Need help creating JSONObject java

I am trying to create the next json object in java dynamically, for example, if the WARN object does not exist, add it or whatever, followed by a new label message object in an additional array.

This is an example of what I'm trying to dynamically build.

{
  "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"
    }
  ]
}

This is what I tried.

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

}

However, this is my result that you see is incorrect. I cannot add a title to the fact that my warnings are. Types are in only one object.

{
  "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"
        }
      ]
    }
  ]
}

I can’t understand how to build this object dynamically, any help would be appreciated.

+4
source share
2 answers

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"
  } ]
}
+3

.

, Google GSON POJO JSON. GSON POJO, , , , . - JSON GSON. JSON, POJO. , :

public class Warnings {
  @SerializedName("WARN")
  private Warn[] warns;
  @SerializedName("NOTIFICATION")
  private Notification[] notifications;

  public WARN[] getWARN(){...}

  public void setWarn(WARN[] warns){...}
  ...
}

public class Warn {
  private String message;
  private String label;
  ... // setters & getters
}

public class Notification {
  private String message;
  private String label;
  ... // setters & getters
}

:

Warnings someWarnings = new Warnings();
// Populate warnings
Gson gson = new Gson();
// Serialize
String jsonString = gson.toJson(someWarnings);
// Deserialize
Warnings sameWarinings = gson.fromJson(jsonString);
0

Source: https://habr.com/ru/post/1530487/


All Articles