我在添加到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 :(得分: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
}