[英]how to get csv from nested json data
Hi I am trying to get csv file from nested json array I am also getting csv but problem is seller_name and drop_seller name is showing as [object,object] because of nested json data attaching image file of my csv file 嗨,我正在尝试从嵌套的json数组中获取csv文件,我也正在获取csv,但问题是Seller_name和drop_seller名称显示为[object,object],因为嵌套的json数据附加了我的csv文件的图像文件
here is my array
这是我的数组
{
"id":"1925",
"booking_id":"1442931120",
"appointment_time":"2015-09-29 16:00:00",
"booking_address":"Sector 15 gurgaon",
"booking_type":"later",
"category_id":"9",
"category_name":"Wash & Iron",
"stage":"accepted",
"lat":"74.83908",
"invoice_price":null,
"lon":"87.7333",
"user_name":" ",
"user_mobile_number":"8800292916",
"user_id":"1782",
"booking_time":"2015-09-22 19:09:55",
"price":null,
"seller_id":"52",
"payment_stage":"individual",
"discount":null,
"visting_charge":"0",
"rating":null,
"promo_code":"",
"drop_address":"",
"drop_time":"2015-10-01 20:18:28",
"comments":"",
"platform":"Web",
"order_return":"0",
"cloths_count":null,
"dc_cloths_count":null,
"cloths_weight":null,
"categories":null,
"service_type":"regular",
"category_type":"0",
"seller":{
"name":"Dinesh",
"mobile_number":"9818782536",
"distance":"10",
"lat":"28.4356578",
"lon":"77.1110703",
"id":"52",
"picture":"resources\/seller\/images\/20150709004010_559e2c4248c73.jpg",
"provider_picture":"resources\/seller\/provider-pictures\/logo_559e2c4248d4a.png"
},
"dropSeller":{
"name":"Dinesh",
"mobile_number":"9818782536",
"distance":"10",
"lat":"28.4356578",
"lon":"77.1110703",
"id":"52",
"picture":"resources\/seller\/images\/20150709004010_559e2c4248c73.jpg",
"provider_picture":"resources\/seller\/provider-pictures\/logo_559e2c4248d4a.png"
}
}
and here is my javascript file 这是我的javascript文件
function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) {
//If JSONData is not an object then JSON.parse will parse the JSON string in an Object
var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
var CSV = '';
//Set Report title in first row or line
CSV += ReportTitle + '\r\n\n';
//This condition will generate the Label/Header
if (ShowLabel) {
var row = "";
//This loop will extract the label from 1st index of on array
for (var index in arrData[0]) {
//Now convert each value to string and comma-seprated
row += index + ',';
}
row = row.slice(0, -1);
//append Label row with line break
CSV += row + '\r\n';
}
//1st loop is to extract each row
for (var i = 0; i < arrData.length; i++) {
var row = "";
//2nd loop will extract each column and convert it in string comma-seprated
for (var index in arrData[i]) {
row += '"' + arrData[i][index] + '",';
}
row.slice(0, row.length - 1);
//add a line break after each row
CSV += row + '\r\n';
}
if (CSV == '') {
alert("Invalid data");
return;
}
//Generate a file name
var fileName = "MyReport_";
//this will remove the blank-spaces from the title and replace it with an underscore
fileName += ReportTitle.replace(/ /g,"_");
//Initialize file format you want csv or xls
var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);
// Now the little tricky part.
// you can use either>> window.open(uri);
// but this will not work in some browsers
// or you will not get the correct file extension
//this trick will generate a temp <a /> tag
var link = document.createElement("a");
link.href = uri;
//set the visibility hidden so it will not effect on your web-layout
link.style = "visibility:hidden";
link.download = fileName + ".csv";
//this part will append the anchor tag and remove it after automatic click
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
and I am passing an json object as earlier I shown and I am getting csv file also but problem is in seller_name and drop_seller name showing as[object,object] rest data coming right I think it's because these value are in nested json how to get these name any help please 而且我像我之前显示的那样传递了一个json对象,我也得到了csv文件,但是问题出在Seller_name和drop_seller名称上,显示为[object,object]剩余数据正确,我想是因为这些值在嵌套json中如何获取这些名字请帮忙
If I understand you correctly you want only the sellers name to be displayed, not every property of it? 如果我理解正确,那么您只希望显示卖方名称,而不是显示其所有属性?
If that's the case simply add a check for an object and then show its name instead. 如果是这种情况,只需添加一个对象检查,然后显示其名称即可。
for (var index in arrData[i]) {
var data = arrData[i][index];
if(typeof data === 'object'){
data = data.name;
}
row += '"' + data + '",';
}
I tried the above solution and found out it does not work well if the field has null value since type of null returns an object. 我尝试了上述解决方案,发现如果字段具有null值,则它不能正常工作,因为null的类型会返回一个对象。 Try the below solution if somebody still looking for a solution.
如果仍在寻找解决方案,请尝试以下解决方案。 Hope this helps!
希望这可以帮助!
for (var index in arrData[i]) {
var data = arrData[i][index];
if(typeof data === 'object' && data != null){
data = data.name;
}
row += '"' + data + '",';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.