patternjavaMinor
CSV to JSON conversion in Java
Viewed 0 times
jsonconversioncsvjava
Problem
Since I've looked far and wide for a good example of this to no avail, I have created my own using the
```
public static JSONArray getJSON(String url) throws IOException, JSONException, URISyntaxException, TransformerException{
HttpClient httpClient = new DefaultHttpClient();
JSONArray jArray = new JSONArray();
Object[] q = null;
Deque queue = new ArrayDeque();
try{
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet);
int responseCode = response.getStatusLine().getStatusCode();
logger.info("Response Code : " + responseCode);
if (responseCode != 404){
logger.info("Response" + response.getEntity().getContent());
try(BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()))) {
if(reader != null){
String aux = "";
while ((aux = reader.readLine()) != null) {
queue.add(aux);
}
q = queue.toArray();
for(int i = 0; i < q.length; i++){
String[] row = q[i].toString().split(",");
JSONObject json = new JSONObject();
json.put("col0", row[0]);
json.put("col1", row[1]);
json.put("col2", row[2]);
json.put("col3", row[3]);
json.put("col4", row[4]);
json.put("col5", row[5]);
jArray.put(json);
}
}
}
}
JSONArray and JSONObject classes. It took me a while to realize that the push method for JSONObject overwrites everything previously pushed.```
public static JSONArray getJSON(String url) throws IOException, JSONException, URISyntaxException, TransformerException{
HttpClient httpClient = new DefaultHttpClient();
JSONArray jArray = new JSONArray();
Object[] q = null;
Deque queue = new ArrayDeque();
try{
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet);
int responseCode = response.getStatusLine().getStatusCode();
logger.info("Response Code : " + responseCode);
if (responseCode != 404){
logger.info("Response" + response.getEntity().getContent());
try(BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()))) {
if(reader != null){
String aux = "";
while ((aux = reader.readLine()) != null) {
queue.add(aux);
}
q = queue.toArray();
for(int i = 0; i < q.length; i++){
String[] row = q[i].toString().split(",");
JSONObject json = new JSONObject();
json.put("col0", row[0]);
json.put("col1", row[1]);
json.put("col2", row[2]);
json.put("col3", row[3]);
json.put("col4", row[4]);
json.put("col5", row[5]);
jArray.put(json);
}
}
}
}
Solution
It would be nice if you provide some information about the libraries used in your code. For example, if I want to read about
The same observation goes for the java classes
Having said that I only have one suggestion to your code. It seems like you don't need to create the array of Objects from the queue of strings (
Instead of adding elements to the
I would add the elements in this other way
JSONArray and JSONObject, which json library do I need to search for?The same observation goes for the java classes
HttpGet, HttpResponse, HttpClient and DefaultHttpClient. Showing the imports of those classes would also be helpful.Having said that I only have one suggestion to your code. It seems like you don't need to create the array of Objects from the queue of strings (
Object[] q = queue.toArray();), you could just iterate over the elements in the queue and then process each element. Instead of adding elements to the
jArray object this waywhile ((aux = reader.readLine()) != null) {
queue.add(aux);
}
q = queue.toArray();
for (int i = 0; i < q.length; i++) {
String[] row = q[i].toString().split(",");
JSONObject json = new JSONObject();
json.put("col0", row[0]);
json.put("col1", row[1]);
json.put("col2", row[2]);
json.put("col3", row[3]);
json.put("col4", row[4]);
json.put("col5", row[5]);
jArray.put(json);
}I would add the elements in this other way
while ((aux = reader.readLine()) != null) {
queue.add(aux);
}
for (String row : queue){
String[] csvValues = row.toString().split(",");
JSONObject json = new JSONObject();
json.put("col0", csvValues[0]);
json.put("col1", csvValues[1]);
json.put("col2", csvValues[2]);
json.put("col3", csvValues[3]);
json.put("col4", csvValues[4]);
json.put("col5", csvValues[5]);
jArray.put(json);
}Code Snippets
while ((aux = reader.readLine()) != null) {
queue.add(aux);
}
q = queue.toArray();
for (int i = 0; i < q.length; i++) {
String[] row = q[i].toString().split(",");
JSONObject json = new JSONObject();
json.put("col0", row[0]);
json.put("col1", row[1]);
json.put("col2", row[2]);
json.put("col3", row[3]);
json.put("col4", row[4]);
json.put("col5", row[5]);
jArray.put(json);
}while ((aux = reader.readLine()) != null) {
queue.add(aux);
}
for (String row : queue){
String[] csvValues = row.toString().split(",");
JSONObject json = new JSONObject();
json.put("col0", csvValues[0]);
json.put("col1", csvValues[1]);
json.put("col2", csvValues[2]);
json.put("col3", csvValues[3]);
json.put("col4", csvValues[4]);
json.put("col5", csvValues[5]);
jArray.put(json);
}Context
StackExchange Code Review Q#105054, answer score: 2
Revisions (0)
No revisions yet.