Search code examples
javajsonsimplejson

JSONParser can't read a JSON file


I'm using a Json file to save some informations about users with this code:

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");
            }

It works fine and return me a JSON with this structure:

{"users":[
      {"files":[],
       "token":"XXXX",
       "email":"XXXX"}
     ]
}

Then I try to add some informations in files[] array with this code above:

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);
            }

My json file now is showing what I want:

{"users":[
      {"files":[
            {"name":"d9f6cd671a384fe59bae29e2718463b5",
             "type":"Apresentação",
             "date":Sat Feb 09 13:48:32 GMT-03:00 2013}
           ],
       "email":"XXXX",
       "token":"XXXX"
      }
     ]
}

No problem until here. But when I try to load my json again with the first code using JSONParser it can't be read, why?? Everything seems to be all right :(


Solution

  • The date should be surrounded by quotes

    {
        "users": [{
            "files":[{
                "name":"d9f6cd671a384fe59bae29e2718463b5",
                "type":"Apresentação",
                "date":"Sat Feb 09 13:48:32 GMT-03:00 2013"
            }],
            "email":"XXXX",
            "token":"XXXX"
        }]
    }