本文介绍了在javascript中将json对象转换为csv格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图将一个javascript对象设置为csv格式 你可以得到想法abt我的Javascript对象,如果你把它放在在线JSON解析器 http://json.parser.online.fr/ 这是我试图wrk它... bt它翻牌.. http:// jsfiddle .net / fHQzC / 11 / 我试图取对应于值term和相应标题的整个值到csv格式预期输出为 时间,2012年12月9日新闻,德国,选举,埃及,革命,日本,地震人,奥巴马,贝克汉姆标题,Pearce Snubs Beckham 时间,12月5日,生日人, Lebron James 新闻,意大利,欧洲2012决赛 职业冠军全国冠军 可以下载csv文件在excel表中我发现在堆栈是nt真的有用... 解决方案您可以尝试 { // Create Object var items = [ {name:Item 1,color:Green,size:X-Large}, {name:Item 2,color:Green,size:X-Large}, {name:Item 3,color:Green }]; //将对象转换为JSON var jsonObject = JSON.stringify(items); //显示JSON $('#json')。text(jsonObject); //将JSON转换为CSV&显示CSV $('#csv')。text(ConvertToCSV(jsonObject)); }); 和函数ConvertToCSV // JSON to CSV Converter function ConvertToCSV(objArray){ var array = typeof objArray!='object'? JSON.parse(objArray):objArray; var str =''; for(var i = 0; i var line =''; for(array [i]中的var index){ if(line!='')line + =',' line + = array [i] [index ]; } str + = line +'\r\\\'; } return str; } 来源 I am trying to convert a javascript object set in to csv formatYou can get the idea abt my Javascript object, if you put it in online JSON parser http://json.parser.online.fr/This is how I tried to wrk it out... bt it flopped.. http://jsfiddle.net/fHQzC/11/I am trying to take the whole values corresponding to the value "term" and corresponding title in to csv formatThe expected output for is like Time,Dec 9, 2012News,Germany,election, Egypt,Revolution, Japan, EarthquakePerson,Obama, BeckhamTitle,Pearce Snubs BeckhamTime,Dec 5, BirthdayPerson, Lebron JamesNews,Italy,Euro 2012 FinalTitle-Heats National Championsand is it possible to download the csv file in excel sheet the one I found in stack was nt really useful... 解决方案 you can try as $(document).ready(function () { // Create Object var items = [ { name: "Item 1", color: "Green", size: "X-Large" }, { name: "Item 2", color: "Green", size: "X-Large" }, { name: "Item 3", color: "Green", size: "X-Large" }]; // Convert Object to JSON var jsonObject = JSON.stringify(items); // Display JSON $('#json').text(jsonObject); // Convert JSON to CSV & Display CSV $('#csv').text(ConvertToCSV(jsonObject)); });and a function ConvertToCSV// JSON to CSV Converter function ConvertToCSV(objArray) { var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray; var str = ''; for (var i = 0; i < array.length; i++) { var line = ''; for (var index in array[i]) { if (line != '') line += ',' line += array[i][index]; } str += line + '\r\n'; } return str; }Source 这篇关于在javascript中将json对象转换为csv格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-29 02:33