2016-11-29 77 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