我使用JSON文件來保存有關與此代碼的用戶一些信息:JSONParser無法讀取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"}
]
}
然後我嘗試在文件[]數組中添加一些信息,上面這段代碼:
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時,它無法被讀取,爲什麼?一切都顯得沒事:(
你能告訴我你的確切錯誤嗎? – kaysush 2013-02-09 17:10:53