Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9266907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:25:34+00:00 2026-06-18T14:25:34+00:00

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

  • 0

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 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:25:35+00:00Added an answer on June 18, 2026 at 2:25 pm

    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"
        }]
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I'm using an ASP request returning a XML file containing some latin characters. By
I am using JSon response to parse title,date content and thumbnail images and place
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.