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.

4
  • what about String line = br.readLine() from BufferedReader(in) and do your conversions with the line within this loop and create your object and add it into your JSONArray? Commented Jan 16, 2012 at 16:40
  • Why don't you read the individual lines, split by comma and then iterate over the array elements? That way you don't have to process every single character yourself. Did you check the run time without the update of the progress bar? If the text is extemely large you'd not notice any change anyways so you might want to do that every X characters/lines. Btw, what is extremely large? Commented Jan 16, 2012 at 16:41
  • Both: I don't know how to do either of those - mind providing a code example in an answer? Thomas: extremely large is about 3000 lines, each with 11 elements per line. Commented Jan 16, 2012 at 16:44
  • Nevermind: problem solved. Question edited. Commented Jan 16, 2012 at 17:09

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

A long time ago I had that, but it was actually slightly slower than what I have now. Back then I had a bunch of arrays with the .csv elements in them that I just put in a JSONArray. But now I have no arrays and just one .csv file, so I don't see how I can even do that now.
Nevermind, I actually got this to work. Thank you very much! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.