2014-03-06 62 views
4

我想動態地在java中構建下面的json對象,例如,如果WARN對象不存在,添加它或任何其他的,然後添加一個新的標籤消息對象到子陣列。需要幫助建立JSONObject java

這是我試圖動態構建的一個例子。

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

我無法弄清楚如何動態構建這個對象,任何幫助將不勝感激。

回答

3

您嘗試創建的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" 
    } ] 
} 
+0

?我喜歡你的方法。 –

+1

你可以使用像Jackson這樣的好庫來序列化/反序列化POJO。 – Neel

+0

我喜歡谷歌GSON。看到我的答案非常簡短的例子。 – Monir

0

這裏的link一個全面的指導。

如果你想減少工作量,你可以使用谷歌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); 
你用得到這個工作什麼的依賴