Pavan Alapati Pavan Alapati - 26 days ago 10
Android Question

Convert JSON to CSV file placed into Sdcard

We are developing

Phonegap
app.We need
SqLite
Db data placed into
SDCard
.We did
sqLite
Data convert into
JSON
String.now we send to
SDCard
.Using this link File API in PhoneGap

We code this json data placed into SDCard

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
//alert("seeeeee1");
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
alert("seeeeee2");
fileSystem.root.getFile("forTEST.csv", {create: true, exclusive: false}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
alert("seeeeee3");
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
alert("set");
writer.onwriteend = function(evt) {
};
writer.write(JSONData);
}


But when we open it's
CSV
file in my mobile.it's
JSON
format

[
{
"items": "BODYLINESUPER",
"descriptions": "100mg",
"umo": "CTN",
"qty": 7,
"price": 1093.55,
"lineamount": 7654.85,
"customerid": "ARU007",
"tdate": "2015-4-7",
"orderId": "Himansu15",
}
]


but We need Table format in
CSV
file.like

items descriptions umo qty price lineamount customerid tdate orderId
BODYLINESUPER 100mg CTN 7 1093.55 7654.85 ARU007 2015-4-7 Himansu15


this line Data send to CSV file

writer.write(JSONData);


Please guide me.

Answer
  1. Please create a model class Item.
  2. Create a array list with same item class type.
  3. Get all data from database in item class object and put in array list.
  4. After pasring json create a csv file in sd card.

5 Write all array list data into csv file.

Comments