2015-10-20 79 views
0

嗨我想從嵌套的json數組獲取csv文件我也獲取csv,但問題是seller_name和drop_seller名稱顯示爲[object,object]因爲嵌套的json數據附加圖像我的CSV文件的文件enter image description here 這裏是我的陣列如何從嵌套json數據獲取csv

{ 
"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" 
} 
} 

,這裏是我的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); 
} 

,我傳遞一個JSON對象如前面我出,我得到的CSV文件還,但問題出現在seller_name和drop_seller中名稱顯示爲[對象,對象]其餘數據來對我認爲這是因爲這些值是在嵌套的JSON如何得到這些名稱任何幫助請

+0

其實我不太瞭解你的代碼。你是否將該對象當作數組處理?期望的輸出應該如何?我的猜測是,你只解析父對象的屬性和它們的值,因此,有兩個對象作爲值,而字符串顯示爲[對象對象]。如果一個屬性本身就是一個對象,那麼你需要建立一個檢查關係,然後深入閱讀這些值。 – Danmoreng

回答

1

如果我正確地理解你,你想只顯示賣家名稱,而不是它的每一個屬性?

如果是這種情況,只需添加一個對象的檢查,然後顯示其名稱。

for (var index in arrData[i]) { 
    var data = arrData[i][index]; 
    if(typeof data === 'object'){ 
     data = data.name; 
    } 
    row += '"' + data + '",'; 
}