Created
May 18, 2012 23:54
-
-
Save andershaig/2728222 to your computer and use it in GitHub Desktop.
JSON to CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
| <html xmlns="http://www.w3.org/1999/xhtml"> | |
| <head> | |
| <title>JSON to CSV</title> | |
| <script src="scripts/json.js" type="text/javascript"></script> | |
| <script type="text/javascript"> | |
| var json3 = { "d": "[{\"Id\":1,\"UserName\":\"Sam Smith\"},{\"Id\":2,\"UserName\":\"Fred Frankly\"},{\"Id\":1,\"UserName\":\"Zachary Zupers\"}]" } | |
| DownloadJSON2CSV(json3.d); | |
| function DownloadJSON2CSV(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]) { | |
| line += array[i][index] + ','; | |
| } | |
| line.slice(0,line.Length-1); | |
| str += line + '\r\n'; | |
| } | |
| window.open( "data:text/csv;charset=utf-8," + escape(str)) | |
| } | |
| </script> | |
| </head> | |
| <body> | |
| <h1>This page does nothing....</h1> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment