2016-11-29 76 views
0

我無法追加到JSON文件。我可以添加新條目,但不能將其插入正確的位置。使用JSON簡單地將條目添加到JSON數組中

代碼:

public static void main(String args[]) throws Exception { 

    { 
     File file = new File("jsonFormatting.json"); 
     if (!file.exists()) { 
      System.out.println("No file"); 
     } else { 
      try { 
       JSONParser parser = new JSONParser(); 
       Object obj = parser.parse(new FileReader("jsonFormatting.json")); 
       JSONObject jsonObject = (JSONObject) obj; 
       JSONArray jsonItemInfo = (JSONArray) jsonObject.get("itemInfo"); 

       JSONObject newObject = new JSONObject(); 

       newObject.put("item", new Integer(10003)); 
       newObject.put("name", "ID10003"); 

       StringWriter out = new StringWriter(); 
       newObject.writeJSONString(out); 
       String jsonText = out.toString(); 
       System.out.println(jsonText); 

       jsonItemInfo.add(newObject); 

       FileWriter fileToWrite = new FileWriter("jsonFormatting.json", true); 
       try { 
        fileToWrite.write(jsonItemInfo.toJSONString()); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       fileToWrite.flush(); 
       fileToWrite.close(); 

      } catch (IOException | ParseException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

JSON文件:

"sampleArray": [ 
    "Element_0", 
    "Element_1" 
], 
"dataPoint_1": 40, 
"dataPoint_2": 500, 
"dataPoint_3": 650, 
"itemInfo": [ 
    { 
     "item": "10001", 
     "name": "ID10001", 
    }, 
    { 
     "item": "10002", 
     "name": "ID10002", 
    } 
] 

我想補充以下到 「itemInfo」:

{ 
     "item": "10003", 
     "name": "ID10003", 
    } 

然而,當我跑我的Java代碼,它將此添加到JSON文件的末尾,而不是在原始2之後插入新條目:

[{"item":"10001","name":"ID10001"},{"item":"10002","name":"ID10002"},{"item":10003,"name":"ID10003"}] 

在此先感謝您提供的任何建議!

+0

經過進一步研究,似乎我不能插入加入到特定位置文件。我將不得不將文件加載到內存中,執行更改,然後重新生成整個JSON並覆蓋原始文件。任何人都可以確認嗎? –

+0

有一個問題,你可以確認你的json文件數據與你粘貼的問題(Json File Section)相同,比我可以看看這個問題? – Bill

+0

確實如同在文件中一樣,但是整個代碼中的包圍括號缺失{}。無論出於何種原因,Stackoverflow的代碼插入不會在塊中接受它。 –

回答

1

我運行這個代碼,它工作正常,你可以測試這個東西在你身邊。如果我理解你的問題是正確的。

public static void main(String args[]) throws Exception { 

      { 
       File file = new File("jsonFormatting.json"); 
       if (!file.exists()) { 
        System.out.println("No file"); 
       } else { 
        try { 
         JSONParser parser = new JSONParser(); 
         Object obj = parser.parse(new FileReader("jsonFormatting.json")); 
         JSONObject jsonObject = (JSONObject) obj; 
         JSONArray jsonItemInfo = (JSONArray) jsonObject.get("itemInfo"); 

         JSONObject newObject = new JSONObject(); 

         newObject.put("item", new Integer(10003)); 
         newObject.put("name", "ID10003"); 

         StringWriter out = new StringWriter(); 
         newObject.writeJSONString(out); 
         String jsonText = out.toString(); 
         System.out.println(jsonText); 

         jsonItemInfo.add(newObject); 
         jsonObject.put("itemInfo", jsonItemInfo); 
         FileWriter fileToWrite = new FileWriter("jsonFormatting.json", false); 
         try { 
          fileToWrite.write(jsonObject.toJSONString()); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
         fileToWrite.flush(); 
         fileToWrite.close(); 

        } catch (Exception e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
      } 
     } 

我jsonFormatting.json文件數據看起來像

{"sampleArray": [ 
    "Element_0", 
    "Element_1" 
], 
"dataPoint_1": 40, 
"dataPoint_2": 500, 
"dataPoint_3": 650, 
"itemInfo": [ 
    { 
     "item": "10001", 
     "name": "ID10001" 
    }, 
    { 
     "item": "10002", 
     "name": "ID10002" 
    } 
] 
} 

和輸出

{ 
    "sampleArray": [ 
     "Element_0", 
     "Element_1" 
    ], 
    "itemInfo": [ 
     { 
      "item": "10001", 
      "name": "ID10001" 
     }, 
     { 
      "item": "10002", 
      "name": "ID10002" 
     }, 
     { 
      "item": 10003, 
      "name": "ID10003" 
     } 
    ], 
    "dataPoint_2": 500, 
    "dataPoint_1": 40, 
    "dataPoint_3": 650 
} 
+0

此更改謝謝比爾,這很好!它看起來像寫入整個文件,但這不是問題。 –

+0

快樂,它適合你! – Bill