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.

0

2 Answers 2

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);

Sign up to request clarification or add additional context in comments.

4 Comments

When we tried var jsonData = JSON.parse(evt.target.result); alert(jsonData ); alert not printed Please help me
For alert to view the data use : alert(JSON.stringify(evt.target.result));
yes when we placed alert(JSON.stringify(evt.target.result)); We got Alert as my image data in my question with " ";
@PavanAlapati JSON.parse function make it as object that's why, may be, it is nor alerting.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.