-1

From server I receive JSON object which is in this format:

[ { "Id": 1, "defNo": "ME-2-17", "ReportDate": "2013-10-04T00:00:00", "Remarks": "" } ]

to use it in the loop I am trying to convert it in an array through $.parseJSON(responseText but after converting I am still unable to use it as console says that:

Uncaught TypeError: Object #<Object> has no method 'join'

Complete Function:

function exportToCsv() {
var formInfo = $("#requestSortForm").serialize();
$.post('../../REQUEST/GetSortedRequest', formInfo, function (responseText) {

    data = $.parseJSON(responseText);
    console.log($.parse(responseText));

    var csvContent = "data:text/csv;charset=utf-8,";
    data.forEach(function (infoArray, index) {

        dataString = infoArray.join(",");
        csvContent += index < infoArray.length ? dataString + "\n" : dataString;

    });
    var encodedUri = encodeURI(csvContent);
    window.open(encodedUri);
});
}

I am following this example from an answer over SO from Here

Community
  • 1
  • 1
Maven
  • 14,587
  • 42
  • 113
  • 174
  • 1
    apparently `infoArray` is an object, and the object doesn't have a join method. the error message states that pretty clearly. – Kevin B Oct 29 '13 at 19:30
  • 1
    because its not an array... – Ahmad Oct 29 '13 at 19:30
  • How can i convert the object into an Array? I have included the example that i am following to make myself a lil clear. Please see the edited answer. – Maven Oct 29 '13 at 19:35

1 Answers1

0

You can use the native JSON.parse() object type, if you're okay with it being a little newer:

var infoArray = [];
var data = JSON.parse(responseText, function(k, v) {
        if (k === "") return v;  infoArray.push(v); return v;
    });

Now you can interact with the values from the JSON as an array and still work with data as the object.

Anthony
  • 36,459
  • 25
  • 97
  • 163