Sma*_*EGA 1 .net c# asp.net-web-api asp.net-web-api2 angular
我有以下格式的数组,我需要将其发布到 API:
console.log(addserverList);
我想将其传递给 api 的 post 方法
const options = {headers: {'Content-Type': 'application/json'}};
return this.http.post( 'http://localhost:54721/api/BulkUpload/SaveBulkUploadData',addserverList,options)
Run Code Online (Sandbox Code Playgroud)
我可以发布到 api,但数据传递始终显示为 NULL
模型 i 的结构如下:
生成数组的函数
private extractData(res: Response) {
let csvData = res['_body'] || '';
let allTextLines = csvData.split(/\r\n|\n/);
let headers = allTextLines[0].split(',');
let lines = [];
for ( let i = 0; i < allTextLines.length; i++) {
// split content based on comma
let data = allTextLines[i].split(',');
if (data.length == headers.length) {
let tarr = [];
for ( let j = 0; j < headers.length; j++) {
tarr.push(data[j]);
}
lines.push(tarr);
}
}
this.csvData = lines;
// console.log(JSON.stringify(this.csvData));
this.saveBulkUpload();
}
Run Code Online (Sandbox Code Playgroud)
您的 JSON 是一个数组的数组。该方法需要一个与类的格式匹配的对象数组AddServer。
您的 JSON 应该如下所示:
[
{
"HostName": "Server1",
"Type1": "type1_1",
"Type2": "type1_1",
},
{
"HostName": "Server2",
"Type1": "type1_2",
"Type2": "type2_2",
}
]
Run Code Online (Sandbox Code Playgroud)
更改为功能
这需要一些猜测,因为我不知道输入到函数中的服务合同,但需要更改的内容在循环中。
for ( let i = 0; i < allTextLines.length; i++) {
// split content based on comma
let data = allTextLines[i].split(',');
if (data.length == headers.length) {
let tobj = {};
tobj.HostName = data[0];
tobj.Type1= data[1];
tobj.Type2= data[2];
lines.push(tobj);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3631 次 |
| 最近记录: |