我正在使用Java Eclipse创建一个将编写和读取JSON文件的事件管理系统。在这里,我有代码创建一个新的JSON文件...
public void actionPerformed(ActionEvent arg0) {
//Declaration of variables
String title = txtTitle.getText();
String month = (String) cboMonth.getSelectedItem();
String day = (String) cboDate.getSelectedItem();
String year = (String) cboYear.getSelectedItem();
String location = txtLocation.getText();
String description = txtDescription.getText();
String URL = txtURL.getText();
// Combine multiple variables together to make a single variable
String date = month + "" + day + "" + year;
// Create a new instance of the class called 'Event'
Event event = new Event();
// Assign values to the getter/setter methods of this instance
event.setName(title);
event.setDate(date);
event.setLocation(location);
event.setDesc(description);
event.setURL(URL);
// Add this new instance to the 'eventList' array list
MainMenu.eventList.add(event);
// Create a new instance of the class called 'Event'
JSONObject JSONEvent = new JSONObject();
// Add data to the JSON file
JSONEvent.put("Title", title);
JSONEvent.put("Date", date);
JSONEvent.put("Location", location);
JSONEvent.put("Description", description);
JSONEvent.put("URL", URL);
// Create a new JSON file called 'Events.json' that has elements added to it
try (FileWriter file = new FileWriter("Events.json", true)) {
file.write("n");
file.write(JSONEvent.toJSONString());
file.flush();
// Error Handling
} catch (IOException e) {
e.printStackTrace();
}
}
通过创建JSON文件并使用JSONOBJECTS填充它,此代码非常好起来。这是JSON文件中的单个条目的图像...单json元素
i然后有一个单独的类带有以下代码,该类别试图读取JSON文件并将其内容输出到控制台...
public static void main(String[] args) {
JSONObject JSONEvent;
String line = null;
try {
FileReader fileReader = new FileReader("Events.json");
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
JSONEvent = (JSONObject) new JSONParser().parse(line);
String title = (String) JSONEvent.get("Title");
System.out.println(title);
String date = (String) JSONEvent.get("Date");
System.out.println(date);
String location = (String) JSONEvent.get("Location");
System.out.println(location);
String description = (String) JSONEvent.get("Description");
System.out.println(description);
String URL = (String) JSONEvent.get("URL");
System.out.println(URL);
Event event = new Event();
event.setName(title);
event.setDate(date);
event.setLocation(location);
event.setDesc(description);
event.setURL(URL);
MainMenu.eventList.add(event);
}
bufferedReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
运行此代码时,我会在控制台中得到以下错误...位置0
的意外令牌文件末端有人知道这个错误是什么意思?
您写入文件的第一件事是一个空行:
file.write("n");
因此,在阅读文件时,您正在尝试将一个空字符串解析为JSON,因此:解析器甚至在有机会解析任何东西之前找到其输入的末尾。
而不是依靠生成的JSON的内部格式,并将几个不同的JSON对象写入文件,而是更简单,更安全地在文件中写下单个对象数组(替换其以前的内容),并在内存中立即读取整个数组。
面临类似的问题。正在尝试从一个Kafka主题的文件中编写JSON数据记录。正在获得Unexpected token END OF FILE at position
。作为修复程序,我清除了文件中的所有n
字符,并尝试将其发送到Kafka主题。它起作用。