I am using the Javascript FileReader. I would like to read in the result of the FileReader and then convert each record into a JS object. Using the following code:
$("#csv").bind("change", function (event) {
var reader = new FileReader();
reader.onload = function (theFile) {
try {
var input = $.csv.toArrays(theFile.target.result);
alert("CSV loaded successfully");
alert(input);
}
catch (e) {
alert("CSV Parse error.");
return;
}
};
reader.readAsText(event.target.files[0]);
});
}
alert(input) will display the entire csv file as a string. How would I go about creating an array of javascript objects or JSON strings from this input? In other words, I would assume I would need to read in the 1st line as the set of properties, then each record thereafter would then create an object with the set of properties and insert each of these objects into an array.
For example, from the following string:
Name,Age,Gender
John,20,Male
I would like to create an object as:
{ Name: "John", Age: 20, Gender: "Male" }
Thanks in advance!