return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>Javascriptconvert json to csv - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascriptconvert json to csv - Stack Overflow

programmeradmin3浏览0评论

How to convert an array of json object to csv ?

ex

[{ name: "Item 1", color: "Green", size: "X-Large" },
 { name: "Item 2", color: "Green", size: "X-Large" },
 { name: "Item 3", color: "Green", size: "X-Large" }];

give

name;color;size
Item 1;Green;X-Large
Item 2;Green;X-Large
Item 3;Green;X-Large

How to convert an array of json object to csv ?

ex

[{ name: "Item 1", color: "Green", size: "X-Large" },
 { name: "Item 2", color: "Green", size: "X-Large" },
 { name: "Item 3", color: "Green", size: "X-Large" }];

give

name;color;size
Item 1;Green;X-Large
Item 2;Green;X-Large
Item 3;Green;X-Large
Share Improve this question asked Mar 13, 2013 at 9:36 FarandoleFarandole 5512 gold badges8 silver badges24 bronze badges 2
  • possible duplicate of How to convert JSON to CSV format and store in a variable – jcubic Commented Mar 13, 2013 at 9:39
  • 1 In my function, I have added header and dateformat are supported – Farandole Commented Mar 13, 2013 at 9:45
Add a ment  | 

3 Answers 3

Reset to default 5

Example in JSFiddle : http://jsfiddle/FLR4v/

Dependencies :

  • Underscorejs http://underscorejs/underscore-min.js
  • Momentjs http://cdnjs.cloudflare./ajax/libs/moment.js/2.0.0/moment.min.js

The function

 /**
 * Return a CSV string from an array of json object
 *
 * @method JSONtoCSV
 * @param {Object} jsonArray an array of json object
 * @param {String} [delimiter=;] delimiter
 * @param {String} [dateFormat=ISO] dateFormat if a date is detected
 * @return {String} Returns the CSV string
**/
function JSONtoCSV(jsonArray, delimiter, dateFormat){
    dateFormat = dateFormat || 'YYYY-MM-DDTHH:mm:ss Z'; // ISO
    delimiter = delimiter || ';' ;

    var body = '';
    // En tete
    var keys = _.map(jsonArray[0], function(num, key){ return key; });
    body += keys.join(delimiter) + '\r\n';
    // Data
    for(var i=0; i<jsonArray.length; i++){
        var item = jsonArray[i];
        for(var j=0; j<keys.length; j++){
            var obj = item[keys[j]] ;
            if (_.isDate(obj)) {                
                body += moment(obj).format(dateFormat) ;
            } else {
                body += obj ;
            }

            if (j < keys.length-1) { 
                body += delimiter; 
            }
        }
        body += '\r\n';
    }

    return body;
}

Even though this is pretty old question, saving my findings here for future researchers.

Javascript now provides the feature of Object.values which can pull all values of a json to an array, which can then be converted to csv using join.

var csvrecord = Object.keys(jsonarray[0]).join(',') + '\n'; 
jsonarray.forEach(function(jsonrecord) {
   csvrecord += Object.values(jsonrecord).join(',') + '\n';
});

Only limitation is that it is still not supported by a few browsers.

function getCSVFromJson(k)
{
var retVal=[];
k.forEach(function(a){
var s=''; 
for(k in a){
    s+=a[k]+';';
}   

retVal.push(s.substring(0,s.length-1));
});
return retVal;
}
发布评论

评论列表(0)

  1. 暂无评论