Last active
March 10, 2019 03:28
-
-
Save beiweiqiang/24561ca1f349d7e6203b9834a2de8286 to your computer and use it in GitHub Desktop.
纯前端, 解析 json 为 csv 文件并触发 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> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
<script src="https://code.jquery.com/jquery-3.1.0.js"></script> | |
</head> | |
<body> | |
<h1>This page does nothing....</h1> | |
<script type="text/javascript"> | |
var json3 = { | |
"count": 2, | |
"items": [{ | |
"title": "Apple iPhone 4S Sale Cancelled in Beijing Amid Chaos (Design You Trust)", | |
"description": "Advertise here with BSA Apple cancelled its scheduled sale of iPhone 4S in one of its stores in China’s capital Beijing on January 13. Crowds outside the store in the Sanlitun district were waiting on queues overnight. There were incidents of scuffle between shoppers and the store’s security staff when shoppers, hundreds of them, were told that the sales [...]Source : Design You TrustExplore : iPhone, iPhone 4, Phone", | |
"link": "http://wik.io/info/US/309201303", | |
"timestamp": 1326439500, | |
"image": null, | |
"embed": null, | |
"language": null, | |
"user": null, | |
"user_image": null, | |
"user_link": null, | |
"user_id": null, | |
"geo": null, | |
"source": "wikio", | |
"favicon": "http://wikio.com/favicon.ico", | |
"type": "blogs", | |
"domain": "wik.io", | |
"id": "2388575404943858468" | |
}, | |
{ | |
"title": "Apple to halt sales of iPhone 4S in China (Fame Dubai Blog)", | |
"description": "SHANGHAI – Apple Inc said on Friday it will stop selling its latest iPhone in its retail stores in Beijing and Shanghai to ensure the safety of its customers and employees. Go to SourceSource : Fame Dubai BlogExplore : iPhone, iPhone 4, Phone", | |
"link": "http://wik.io/info/US/309198933", | |
"timestamp": 1326439320, | |
"image": null, | |
"embed": null, | |
"language": null, | |
"user": null, | |
"user_image": null, | |
"user_link": null, | |
"user_id": null, | |
"geo": null, | |
"source": "wikio", | |
"favicon": "http://wikio.com/favicon.ico", | |
"type": "blogs", | |
"domain": "wik.io", | |
"id": "16209851193593872066" | |
} | |
] | |
}; | |
const items = json3.items | |
const replacer = (key, value) => value === null ? '' : value // specify how you want to handle null values here | |
const header = Object.keys(items[0]) | |
let csv = items.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(',')) | |
csv.unshift(header.join(',')) | |
csv = csv.join('\r\n') | |
var link = document.createElement("a"); | |
link.id="lnkDwnldLnk"; | |
document.body.appendChild(link); | |
blob = new Blob([csv], { type: 'text/csv' }); | |
var csvUrl = window.webkitURL.createObjectURL(blob); | |
console.log(csvUrl) | |
var filename = 'UserExport.csv'; | |
jQuery("#lnkDwnldLnk") | |
.attr({ | |
'download': filename, | |
'href': csvUrl | |
}); | |
// jQuery('#lnkDwnldLnk')[0].click(); | |
// document.body.removeChild(link); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment