2

We developing Phone-Gap app.We need Get CSV file data and into sqlite DB.We get data FILE API in phoneGap We code like this

function readDataUrl(file) {



    var reader = new FileReader();
    reader.onloadend = function(evt) {
        console.log("Read as data URL");

        console.log(evt.target.result);

    };
    reader.readAsDataURL(file);
}

function readAsText(file) {

    console.log('in readAsText'+file);

    var reader = new FileReader();
    reader.onloadend = function(evt) {
        ss=evt.target.result;
        alert(ss);
          console.log("Read as text");
        console.log(evt.target.result);
    };
    reader.readAsText(file);
}

We get Like this enter image description here

But this format not insert into sqLite DB .We need JSON data.

We need like this

       [  
            {  
                "FOC":1,
                "price":0,
                "customerid":"ARU005",
                "lineamount":0,
                "items":"W.D.M.W HERBAL 250Mx24",
                "tdate":"2015-4-7",
                "qty":8,
                "orderId":"Himansu16:23:20020",
                "umo":"CTN",
                "descriptions":"100mg",
                "bookorder":"ABCARU0052320"
            }
        ]


    [  
    {  
        "TOTALAMOUNT":1000,
        "DISCOUNT":1,
        "NETAMOUNT ":900,
        "VAT":0,
        "GROSSAMOUNT ":900,
        "BOOKORDER":"ABCARUOO7451",
        "CUSTOMERID":"ARU007",
        "TODAYDATE":"2015-4-7"
    }
]

Please guide me.We get data form CSV file.NOW We need getting data convert into JSON so Please help me.

Pavan Alapati
  • 267
  • 3
  • 5
  • 15

2 Answers2

2

Use JSON.parse function of javascript:

With your variable:

var jsonData = JSON.parse(evt.target.result);

For more check this link.

If you are getting an array in the result then you can use: JSON.stringify(evt.target.result);

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
0

This Post can help you.

from linked post you have two options:

  1. Use jquery-csv Plugin to parse CSV file.
  2. Second option is to write function in javascript (Code is given here) that will parse the CSV file.

Now choice is yours. You may need to dig into these two options to get the correct results as the CSV file content you showed in Question is bit different and Uncommon.

Community
  • 1
  • 1
Puneet
  • 2,051
  • 9
  • 17