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

  • Home
  • SEARCH
  • 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 7061009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:25:18+00:00 2026-05-28T04:25:18+00:00

NOTE : My specific problem is solved (explained at the bottom), but I’ll leave

  • 0

NOTE: My specific problem is solved (explained at the bottom), but I’ll leave my question up if any future visitors would like to contribute/look at this question.

Does anyone have any more efficient idea of how I could convert a .csv file into a JSONArray-based string (in other words, convert a .csv file to a JSONArray’s .toString() version)?

This is my (extremely inefficient yet simple) algorithm:

StringBuilder sb = new StringBuilder();
sb.append("[");
boolean firstJson = true;
String toSendOut;

for(int i = 0; i < textToSession.length(); i++){
     if(firstJson) {
          sb.append("[\"");
          sb.append(textToSession.charAt(i));
          firstJson = false;
     } else if (textToSession.charAt(i) == ' ') {
          continue;
     } else if (textToSession.charAt(i) == ',') {
          sb.append("\",\"");
     } else if (textToSession.charAt(i) == '\n') {
          if(i == textToSession.length() - 1) {
               sb.append("\"]");
          } else {
               sb.append("\"],");
          }
          firstJson = true;
     } else {
          sb.append(textToSession.charAt(i)); 
     }

     partialProg = 100.0 * ( (double)(i+1) / (double)(textToSession.length()));
     dia.setProgress((int) partialProg);

}
sb.append("]");
toSendOut = sb.toString();

It is in an AsyncTask with a ProgressDialog to show how fast it is going. However, as textToSession gets extremely large (about 3,000 lines with 11 elements per line in the .csv file), it takes forever for my code to complete. Is there any other efficient way to do this conversion?

Note:
If my code is confusing, it basically will take something like this (a .csv file):

0.0, 5.1, 221\n125.1, 2352.7, 591\n

And transform it into this (a JSONArray after it has been .toString()ed):

[[“0.0″,”5.1″,”221”],[“125.1″,”2352.7″,”591”]]


SOLUTION:
Rather than trying to convert a .csv file to a JSONArray, every time I recorded data (the elements of the .csv file), I newed a JSONArray (named dataPoints) and threw the data in there with multiple .put()s. After that, I had a global JSONArray (named dataSet) that I put dataPoints into. This solved my problem, rather than writing an entire .csv file and then trying to convert it.

  • 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-05-28T04:25:19+00:00Added an answer on May 28, 2026 at 4:25 am

    Why won’t you just put items one by one directly in JSONArray using its put methods? It could do its toString() really fast.
    Besides, I would read your file line by line rather than by 1 char.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note This is not a REBOL-specific question. You can answer it in any language.
I would like to make all .note elements have a specific tooltip. Is it
NOTE This was flagged as a potentially subjective question, but it is not subjective.
I am not sure if my problem is platform specific, but I think it
NOTE: XMLIgnore is NOT the answer! OK, so following on from my question on
Note: Originally this question was asked for PostgreSQL, however, the answer applies to almost
Note that I am not asking which to choose (MVC or MVP), but rather
Note : The code in this question is part of deSleeper if you want
Note The question below was asked in 2008 about some code from 2003. As
On the heels of a specific problem , a self-answer and comments to it,

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.