2016-04-21 30 views
0

我從寫由(JSON (from HTML Table) to CSV)StackExchange成員的應用程序API端點轉換一個可怕的JSON報告這真棒腳本。它在運行代碼片段中完美運行,但是我想將它放到HTML頁面中。談到代碼片段插入到HTML頁面

我曾嘗試添加標題:

<!DOCTYPE html> 
<html> 
<head> 
<title>Convert JSON Report to Table</title> 
</head> 
<body> 

<script type="text/javascript"> 

,並解決在底部的腳本,身體和HTML,但它不會做任何事情。

下面是我想在HTML運行該腳本。我將如何把它放到一個空白的HTML頁面?謝謝,馬特

var data = { 
    "Header": { 
    "Time": "2016-03-30T16:10:19-07:00", 
    "ReportName": "GeneralLedger", 
    "ReportBasis": "Accrual", 
    "StartPeriod": "2016-01-01", 
    "EndPeriod": "2016-03-31", 
    "Currency": "GBP", 
    "Option": [{ 
     "Name": "NoReportData", 
     "Value": "false" 
    }] 
    }, 
    "Columns": { 
    "Column": [{ 
     "ColTitle": "Date", 
     "ColType": "tx_date" 
    }, { 
     "ColTitle": "Transaction Type", 
     "ColType": "txn_type" 
    }, { 
     "ColTitle": "No.", 
     "ColType": "doc_num" 
    }, { 
     "ColTitle": "Name", 
     "ColType": "name" 
    }, { 
     "ColTitle": "Memo/Description", 
     "ColType": "memo" 
    }, { 
     "ColTitle": "Split", 
     "ColType": "split_acc" 
    }, { 
     "ColTitle": "Amount", 
     "ColType": "subt_nat_amount" 
    }, { 
     "ColTitle": "Balance", 
     "ColType": "rbal_nat_amount" 
    }] 
    }, 
    "Rows": { 
    "Row": [{ 
     "Header": { 
     "ColData": [{ 
      "value": "Current", 
      "id": "144" 
     }, { 
      "value": "" 
     }, { 
      "value": "" 
     }, { 
      "value": "" 
     }, { 
      "value": "" 
     }, { 
      "value": "" 
     }, { 
      "value": "" 
     }, { 
      "value": "" 
     }] 
     }, 
     "Rows": { 
     "Row": [{ 
      "ColData": [{ 
      "value": "2016-03-16" 
      }, { 
      "value": "Bill Payment (Cheque)", 
      "id": "181" 
      }, { 
      "value": "1" 
      }, { 
      "value": "Teddy's T Shirt Supplier", 
      "id": "70" 
      }, { 
      "value": "104478" 
      }, { 
      "value": "Creditors", 
      "id": "138" 
      }, { 
      "value": "-600.0" 
      }, { 
      "value": "-600.0" 
      }], 
      "type": "Data" 
     }, { 
      "ColData": [{ 
      "value": "2016-03-17" 
      }, { 
      "value": "Bill Payment (Cheque)", 
      "id": "184" 
      }, { 
      "value": "2" 
      }, { 
      "value": "Teddy's T Shirt Supplier", 
      "id": "70" 
      }, { 
      "value": "104478" 
      }, { 
      "value": "Creditors", 
      "id": "138" 
      }, { 
      "value": "-120.0" 
      }, { 
      "value": "-720.0" 
      }], 
      "type": "Data" 
     }, { 
      "ColData": [{ 
      "value": "2016-03-17" 
      }, { 
      "value": "Deposit", 
      "id": "180" 
      }, { 
      "value": "" 
      }, { 
      "value": "", 
      "id": "" 
      }, { 
      "value": "Opening Balance" 
      }, { 
      "value": "Opening Balance Equity", 
      "id": "137" 
      }, { 
      "value": "2400.0" 
      }, { 
      "value": "1680.0" 
      }], 
      "type": "Data" 
     }, { 
      "ColData": [{ 
      "value": "2016-03-23" 
      }, { 
      "value": "Payment", 
      "id": "186" 
      }, { 
      "value": "345678" 
      }, { 
      "value": "Maxamillion Enterprises", 
      "id": "68" 
      }, { 
      "value": "" 
      }, { 
      "value": "Debtors", 
      "id": "140" 
      }, { 
      "value": "216.0" 
      }, { 
      "value": "1896.0" 
      }], 
      "type": "Data" 
     }] 
     } 
    }] 
    } 
}; 

function parse(data) { 
    var rows = [], 
    row, curRow, rowSegment; 
    for (var i = 0; i < data.Rows.Row.length; ++i) { 
    rowSegment = data.Rows.Row[i].Rows.Row; 
    for (var j = 0; j < rowSegment.length; ++j) { 
     row = []; 
     curRow = rowSegment[j].ColData; 
     for (var x = 0; x < curRow.length; ++x) { 
     row.push(curRow[x].value); 
     } 
     rows.push(row); 
    } 
    } 
    return rows; 
} 

var parsed = parse(data); 
var rowEl, outEl = document.getElementById('html-out'), 
    val; 

for (var i = 0; i < parsed.length; ++i) { 
    rowEl = document.createElement("div"); 
    rowEl.setAttribute("class", "row"); 
    rowEl.appendChild(document.createTextNode(parsed[i].join(', '))); 
    outEl.appendChild(rowEl); 
} 
+1

請出示整個HTML頁面你有。如果您查看[JavaScript控制檯](http://webmasters.stackexchange.com/questions/8525/how-to-open-the-javascript-console-in-different-browsers)並告訴它是否也會有所幫助你會在那裏看到任何錯誤消息。 – JJJ

回答

0

你錯過了調用該函數解析(數據HTML出 div元素

var data = { 
 
    "Header": { 
 
     "Time": "2016-03-30T16:10:19-07:00", 
 
     "ReportName": "GeneralLedger", 
 
     "ReportBasis": "Accrual", 
 
     "StartPeriod": "2016-01-01", 
 
     "EndPeriod": "2016-03-31", 
 
     "Currency": "GBP", 
 
     "Option": [{ 
 
     "Name": "NoReportData", 
 
     "Value": "false" 
 
     }] 
 
    }, 
 
    "Columns": { 
 
     "Column": [{ 
 
     "ColTitle": "Date", 
 
     "ColType": "tx_date" 
 
     }, { 
 
     "ColTitle": "Transaction Type", 
 
     "ColType": "txn_type" 
 
     }, { 
 
     "ColTitle": "No.", 
 
     "ColType": "doc_num" 
 
     }, { 
 
     "ColTitle": "Name", 
 
     "ColType": "name" 
 
     }, { 
 
     "ColTitle": "Memo/Description", 
 
     "ColType": "memo" 
 
     }, { 
 
     "ColTitle": "Split", 
 
     "ColType": "split_acc" 
 
     }, { 
 
     "ColTitle": "Amount", 
 
     "ColType": "subt_nat_amount" 
 
     }, { 
 
     "ColTitle": "Balance", 
 
     "ColType": "rbal_nat_amount" 
 
     }] 
 
    }, 
 
    "Rows": { 
 
     "Row": [{ 
 
     "Header": { 
 
      "ColData": [{ 
 
      "value": "Current", 
 
      "id": "144" 
 
      }, { 
 
      "value": "" 
 
      }, { 
 
      "value": "" 
 
      }, { 
 
      "value": "" 
 
      }, { 
 
      "value": "" 
 
      }, { 
 
      "value": "" 
 
      }, { 
 
      "value": "" 
 
      }, { 
 
      "value": "" 
 
      }] 
 
     }, 
 
     "Rows": { 
 
      "Row": [{ 
 
      "ColData": [{ 
 
       "value": "2016-03-16" 
 
      }, { 
 
       "value": "Bill Payment (Cheque)", 
 
       "id": "181" 
 
      }, { 
 
       "value": "1" 
 
      }, { 
 
       "value": "Teddy's T Shirt Supplier", 
 
       "id": "70" 
 
      }, { 
 
       "value": "104478" 
 
      }, { 
 
       "value": "Creditors", 
 
       "id": "138" 
 
      }, { 
 
       "value": "-600.0" 
 
      }, { 
 
       "value": "-600.0" 
 
      }], 
 
      "type": "Data" 
 
      }, { 
 
      "ColData": [{ 
 
       "value": "2016-03-17" 
 
      }, { 
 
       "value": "Bill Payment (Cheque)", 
 
       "id": "184" 
 
      }, { 
 
       "value": "2" 
 
      }, { 
 
       "value": "Teddy's T Shirt Supplier", 
 
       "id": "70" 
 
      }, { 
 
       "value": "104478" 
 
      }, { 
 
       "value": "Creditors", 
 
       "id": "138" 
 
      }, { 
 
       "value": "-120.0" 
 
      }, { 
 
       "value": "-720.0" 
 
      }], 
 
      "type": "Data" 
 
      }, { 
 
      "ColData": [{ 
 
       "value": "2016-03-17" 
 
      }, { 
 
       "value": "Deposit", 
 
       "id": "180" 
 
      }, { 
 
       "value": "" 
 
      }, { 
 
       "value": "", 
 
       "id": "" 
 
      }, { 
 
       "value": "Opening Balance" 
 
      }, { 
 
       "value": "Opening Balance Equity", 
 
       "id": "137" 
 
      }, { 
 
       "value": "2400.0" 
 
      }, { 
 
       "value": "1680.0" 
 
      }], 
 
      "type": "Data" 
 
      }, { 
 
      "ColData": [{ 
 
       "value": "2016-03-23" 
 
      }, { 
 
       "value": "Payment", 
 
       "id": "186" 
 
      }, { 
 
       "value": "345678" 
 
      }, { 
 
       "value": "Maxamillion Enterprises", 
 
       "id": "68" 
 
      }, { 
 
       "value": "" 
 
      }, { 
 
       "value": "Debtors", 
 
       "id": "140" 
 
      }, { 
 
       "value": "216.0" 
 
      }, { 
 
       "value": "1896.0" 
 
      }], 
 
      "type": "Data" 
 
      }] 
 
     } 
 
     }] 
 
    } 
 
    }; 
 

 
    function parse(data) { 
 
    var rows = [], 
 
     row, curRow, rowSegment; 
 
    for (var i = 0; i < data.Rows.Row.length; ++i) { 
 
     rowSegment = data.Rows.Row[i].Rows.Row; 
 
     for (var j = 0; j < rowSegment.length; ++j) { 
 
     row = []; 
 
     curRow = rowSegment[j].ColData; 
 
     for (var x = 0; x < curRow.length; ++x) { 
 
      row.push(curRow[x].value); 
 
     } 
 
     rows.push(row); 
 
     } 
 
    } 
 
    return rows; 
 
    } 
 
    var parsed = parse(data); 
 
    var rowEl, outEl = document.getElementById('html-out'), 
 
    val; 
 
    for (var i = 0; i < parsed.length; ++i) { 
 
    rowEl = document.createElement("div"); 
 
    rowEl.setAttribute("class", "row"); 
 
    rowEl.appendChild(document.createTextNode(parsed[i].join(', '))); 
 
    outEl.appendChild(rowEl); 
 
    } 
 

 
    parse(data);
<div id="html-out"> 
 

 
</div>

+0

完美,謝謝 - 無法解決它爲什麼不工作 - 真棒:) –