Since I've looked far and wide for a good example of this to no avail, I have created my own using the 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<String> queue = new ArrayDeque<String>();
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);
}
}
}
}
}
return null;
}
return jArray;
}finally{
httpClient.getConnectionManager().shutdown();
}
}
-
3\$\begingroup\$ The amazing Jackson library supports CSV data format as well. \$\endgroup\$Simon Forsberg– Simon Forsberg2015年09月19日 21:27:40 +00:00Commented Sep 19, 2015 at 21:27
2 Answers 2
It would be nice if you provide some information about the libraries used in your code. For example, if I want to read about 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 way
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);
}
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);
}
Try-with-resources
You should use try-with-resources when you have the opportunity if you HTTP library implements AutoCloseable
for their connections. I think that HttpGet
do implement the interface so it would be easier to not let connections leaked.
Magic Number
if (responseCode != 404){
404
is a known return code for HTTP and normally every good library will provide you with a classes that will have the constant for return code. Apache have HttpStatus
with SC_NOT_FOUND
representing 404
Methods
Don't hesitate to refine your code into smaller methods. This will help fleshed out individual part of the code and make it easier to read the code.
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]);
This would have been a perfect candidate to be a method createJsonObject
which return the created object. You could have use a loop here too since you're repeating the same line 5 times (maybe more or less later if your format change)
Formatting
Your code is in a weird format, I don't know if you had a hard time with the format in the question or if your code is like this, but note that you're not following a standard that I know. Using a common standard will go a long way to help the readability of your code.