我正在使用 Json 文件通过以下代码保存有关用户的一些信息:
Login login = new Login(email, token, jsessionId);
JSONObject jsonObj = new JSONObject();
JSONObject userDetail = new JSONObject();
JSONArray listFiles = new JSONArray();
userDetail.put("email", email);
token = EncodeUtil.encode(token);
userDetail.put("token", token);
userDetail.put("files", listFiles);
try {
if (!(file.exists())) {
JSONArray userDetails = new JSONArray();
userDetails.add(userDetail);
jsonObj.put("users", userDetails);
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(jsonObj.toString());
fileWriter.flush();
fileWriter.close();
} else {
boolean emailExists = true;
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader(file));
JSONObject jsonObjReader = (JSONObject) obj;
JSONArray jsonArrayWriter = (JSONArray) jsonObjReader.get("users");
int length = jsonArrayWriter.size();
for (int i = 0; i < length; i++) {
Object currentObj = jsonArrayWriter.get(i);
JSONObject currentJson = (JSONObject) currentObj;
String email2 = (String) currentJson.get("email");
if (email2.equals(email)) {
emailExists = true;
} else {
emailExists = false;
}
}
if (!emailExists) {
jsonArrayWriter.add(userDetail);
jsonObj.put("users", jsonArrayWriter);
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(jsonObj.toString());
fileWriter.flush();
fileWriter.close();
}
}
} catch (Exception e) {
Logger.getLogger(LoginLocalServiceImpl.class.getName()).log(Level.SEVERE, e.getMessage());
throw new PortalException("Erro ao ler Json File");
}
它工作正常并返回一个具有这种结构的 JSON:
{"users":[
{"files":[],
"token":"XXXX",
"email":"XXXX"}
]
}
然后我尝试使用上面的代码在 files[] 数组中添加一些信息:
try {
JSONObject fileRef = new JSONObject();
fileRef.put("date", date);
fileRef.put("type", type);
fileRef.put("name", name);
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader(file));
JSONObject jsonObjReader = (JSONObject) obj;
JSONArray jsonArrayUsers = (JSONArray) jsonObjReader.get("users");
for (int i = 0; i < jsonArrayUsers.size(); i++) {
JSONObject jsonTest = (JSONObject) jsonArrayUsers.get(i);
String emailTest = (String) jsonTest.get("email");
if (emailTest.equals(email)) {
JSONArray jsonArrayFiles = (JSONArray) jsonTest.get("files");
jsonArrayFiles.add(fileRef);
// jsonTest.put("files", jsonArrayFiles);
// jsonArrayUsers.add(jsonTest);
// jsonObjReader.put("users", jsonArrayUsers);
// System.out.println(jsonArrayFiles.toString());
}
}
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(jsonObjReader.toString());
fileWriter.flush();
fileWriter.close();
} catch (IOException ex) {
Logger.getLogger(MainViewController.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParseException ex) {
Logger.getLogger(MainViewController.class.getName()).log(Level.SEVERE, null, ex);
}
我的 json 文件现在显示了我想要的内容:
{"users":[
{"files":[
{"name":"d9f6cd671a384fe59bae29e2718463b5",
"type":"Apresentação",
"date":Sat Feb 09 13:48:32 GMT-03:00 2013}
],
"email":"XXXX",
"token":"XXXX"
}
]
}
到这里为止没问题。但是,当我尝试使用 JSONParser 使用第一个代码再次加载我的 json 时,它无法读取,为什么?一切似乎都很好:(