我收到来自后端的特殊字符的回复。如果我正在形成后端,那么这就是响应。
注意:此响应来自动态存储过程(PIVOT表)表单,因此响应具有动态键。根据我们传递给响应的参数,密钥将被更改。我们每次都不会获得相同的JSON密钥。 JSON响应包含两列,有时它包含9列。
[
{
"Layer_Nbr": "4",
"Barbican B [Dec 28 2017 2:47PM]": "1",
"BESIM [Dec 28 2017 2:47PM]": "1",
"TPRET18 Ba [Dec 28 2017 1:47PM]": null,
"TPRET18 Ba [Dec 28 2017 2:10PM]": "1",
"TPRET18 Ba [Dec 28 2017 2:11PM]": "1",
"TPRET18 Ba [Dec 28 2017 2:17PM]": "1",
"TPRET18 Ba [Dec 28 2017 2:18PM]": "1",
"TPRET18 Ba [Dec 28 2017 2:26PM]": null
},
{
"Layer_Nbr": "5",
"Barbican B [Dec 28 2017 2:47PM]": null,
"BESIM [Dec 28 2017 2:47PM]": null,
"TPRET18 Ba [Dec 28 2017 1:47PM]": null,
"TPRET18 Ba [Dec 28 2017 2:10PM]": null,
"TPRET18 Ba [Dec 28 2017 2:11PM]": null,
"TPRET18 Ba [Dec 28 2017 2:17PM]": null,
"TPRET18 Ba [Dec 28 2017 2:18PM]": null,
"TPRET18 Ba [Dec 28 2017 2:26PM]": null
},
{
"Layer_Nbr": "6",
"Barbican B [Dec 28 2017 2:47PM]": null,
"BESIM [Dec 28 2017 2:47PM]": null,
"TPRET18 Ba [Dec 28 2017 1:47PM]": null,
"TPRET18 Ba [Dec 28 2017 2:10PM]": null,
"TPRET18 Ba [Dec 28 2017 2:11PM]": null,
"TPRET18 Ba [Dec 28 2017 2:17PM]": null,
"TPRET18 Ba [Dec 28 2017 2:18PM]": null,
"TPRET18 Ba [Dec 28 2017 2:26PM]": null
},
{
"Layer_Nbr": "7",
"Barbican B [Dec 28 2017 2:47PM]": "1",
"BESIM [Dec 28 2017 2:47PM]": "1",
"TPRET18 Ba [Dec 28 2017 1:47PM]": "1",
"TPRET18 Ba [Dec 28 2017 2:10PM]": "1",
"TPRET18 Ba [Dec 28 2017 2:11PM]": "1",
"TPRET18 Ba [Dec 28 2017 2:17PM]": "1",
"TPRET18 Ba [Dec 28 2017 2:18PM]": "1",
"TPRET18 Ba [Dec 28 2017 2:26PM]": "1"
}
]
我通过我的服务消费了这个回复
getProgramCurvePivot(Key: number, ScenarioKey: number): Observable<any> {
return this._http.get(ENDPOINT + 'GetProgramCurvePivot/?id=' + Key + '¶m1=' + ScenarioKey)
.map((response: Response) =>
<any>response.json())
.catch((err) => { throw err; });
}
我订阅了programComponent
中的响应getProgramCurve() {
this.Service.getProgramCurvePivot(this.Key, this.ScenarioKey)
.subscribe(response => {
this.programCurvedata = response;
},
error => {
this.loggingService.sendToServerError(error);
});
}
我正在尝试从JSON响应键中替换空格和特殊字符,因为kendo网格不接受带有特殊字符和空格的字段标题,因此它会发出警告,因为它不是有效的Javascript标识符。
当我在我的组件中订阅响应时,你能帮我解决如何替换数组中的特殊字符和空格(this.programCurvedata
)吗?
我在单元格编辑中使用 kendo grid ui angular 。
这是我的HTML代码
<div class="col-lg-5 col-md-5 col-sm-5 col-xs-8" *ngIf="programCurvedata">
<kendo-grid #grid [data]="programCurvedata" [resizable]="true"
(cellClick)="cellClickHandler($event)"
(cellClose)="cellCloseHandler($event)" >
<kendo-grid-column *ngFor="let item of getProgramNameList();let index=index" width="23%"
title="{{item}}" field="{{item}}">
<ng-template kendoGridCellTemplate let-dataItem>
<div> {{ dataItem[item] }} </div>
</ng-template>
</kendo-grid-column>
</kendo-grid>
</div>
getProgramNameList() {
if (this.programCurvedata.length) {
const programList = Object.keys(this.programCurvedata[0]);
programList.splice(0, 1);
return programList;
}
}
答案 0 :(得分:1)
这将删除所有非字母数字字符,因此比删除空格更强大......
var data = [{
"Layer_Nbr": "4",
"Barbican B [Dec 28 2017 2:47PM]": "1",
"BESIM [Dec 28 2017 2:47PM]": "1",
"TPRET18 Ba [Dec 28 2017 1:47PM]": null,
"TPRET18 Ba [Dec 28 2017 2:10PM]": "1",
"TPRET18 Ba [Dec 28 2017 2:11PM]": "1",
"TPRET18 Ba [Dec 28 2017 2:17PM]": "1",
"TPRET18 Ba [Dec 28 2017 2:18PM]": "1",
"TPRET18 Ba [Dec 28 2017 2:26PM]": null
}];
// parse the object's properties - add one with a safer name and delete the old one
function makeObjectKeysSafe(obj) {
Object.keys(obj).forEach(function(key) {
var newKey = key.replace(/\W/g, "");
obj[newKey] = obj[key];
delete obj[key];
});
}
data.forEach(function(o) { makeObjectKeysSafe(o); });
console.log(data);
&#13;