Reputation: 10698
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 new
ed 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.
Upvotes: 0
Views: 823
Reputation: 10014
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.
Upvotes: 1