将JSON转换为放入Sdcard中的CSV文件

Convert JSON to CSV file placed into Sdcard

本文关键字:Sdcard 中的 CSV 文件 JSON 转换      更新时间:2023-09-26

我们正在开发Phonegap应用程序。我们需要将SqLite Db数据放入SDCard。我们将sqLite数据转换为JSON字符串。现在我们将其发送到SDCard。在PhoneGap 中使用此链接文件API

我们将这个json数据编码到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);
}

但当我们打开我手机中的CSV文件时,它是JSON格式的

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

但我们需要CSV文件中的表格格式。类似

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

此行数据发送到CSV文件

writer.write(JSONData);

请引导我。

  1. 请创建一个模型类Item
  2. 创建具有相同项类类型的数组列表
  3. 从数据库中获取项类对象中的所有数据,并将其放入数组列表中
  4. 在pasring json之后,在sd卡中创建一个csv文件

5将所有数组列表数据写入csv文件。