【发布时间】:2020-09-02 09:45:21
【问题描述】:
我在读取我的json文件时遇到了麻烦目录:E:\1PROGRAMMING\Padlock\src\padlock\register\info.json
{
"users": [
{
"Email": "3",
"Second Name": "2",
"Age": "123",
"Name": "1",
"Password": "4"
}
]
}
问题是,每次我尝试将我的内容文件作为 json 对象读取时,我都会收到类似帖子标题的错误。 主要思想是读取这个 json 文件,在我的 json 对象内的“users”数组中添加一个新用户,并创建一个基本的本地用户数据库。
private static void SignUp() throws IOException, JSONException, ParseException {
//System.out.println("SignUp");
String path = "E:\\1PROGRAMMING\\Padlock\\src\\padlock\\register\\info.json";
String[] labels = {"Age","Name","Second Name","Email","Password"};
ArrayList<String> dataUser = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
//check if value Integer
System.out.println(labels[0]);
int age = Integer.parseInt(scanner.nextLine());
if( age >= 18) {
dataUser.add(Integer.toString(age)); //adding age data to arraylist as first data
for (int element =1;element< labels.length;element++){ //adding rest of data
System.out.println(labels[element]);
String data = scanner.nextLine();
dataUser.add(data);
}
/////////////////////////////////////////////////
//Spring data request to Python serverless
/////////////////////////////////////////////////
System.out.println(dataUser);
//Add to JSON file
File exists = new File(path);
if (exists.exists()) {//check if json exists
System.out.println("File found.");
//addToJson()
addToJson(path, dataUser); //HERE IS THE PROBLEM
}else{
System.out.println("File not found... Creating File with the new user");
//createJson()
createJson(path, dataUser, labels);
//createJson(path, name, secondName,age,email,password);
}
}else {
System.out.println("You must be more than 18 years old.");
System.exit(0);
}
}
还有我想读取和编辑文件的 addToJson 函数
private static void addToJson(String path, ArrayList<String> dataUser) throws IOException, ParseException, JSONException {
//create jsonobject to add in our path file
//read path file content
JSONObject ar = new JSONObject(path);
for (int i = 0; i < ar.length(); i++) {
System.out.println( "Name: " + ar.getString("Password") );
}
//Add jsonobject created into our path file
}
它会绘制此错误消息:
** 线程“main”org.json.JSONException 中的异常:JSONObject 文本必须在 E:\1PROGRAMMING\Padlock\src\padlock\register\info.json 的字符 1 处以 '{' 开头 **
【问题讨论】:
-
已经发布了一个答案,如果它对你有用,请告诉我。