CSV or JSON file from List

So, I have populated a List from an internally stored SQLite table. I would like to send this list to a web service. Can I send the list itself or should I store it in a JSONarray or CSV file? Is there an easy way of converting the List to either of these. Please provide some sample code as I am having trouble coming up with it.

Answers


In the interest of following standards, I believe you should definitely use JSON. If its a list, its very easy to construct the JSON with simple string concatenation.

I imagine the JSON would look like

{ col1:"Value1", col2:"Value2", col3:"Value3" }

For JSON, You could try the Gson library for ease of conversion. The Jackson library might be more efficient. Since you asked for an example, the following is a snippet from the Gson page

Collections Examples

Gson gson = new Gson();
Collection<Integer> ints = Lists.immutableList(1,2,3,4,5);

(Serialization)

String json = gson.toJson(ints); ==> json is [1,2,3,4,5]

(Deserialization)

Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);

Otherwise have a look at protobufs - probably more efficient.

On android you might have issues with Gson speeds in certain situations but it should work


Need Your Help