所以我來自C#的背景,我可以在動態和反射的方式做事情,我試圖將它應用於我正在編寫的TypeScript類。 一些背景知識,我正在將應用程序轉換爲一個Web應用程序,後端開發人員不想完全改變後端以適應Json。所以,他會送我回來的JSON看起來像這樣:TypeScript/Angular 2創建一個動態對象解串器
{
Columns: [
{
"ColumnName": "ClientPK",
"Label": "Client",
"DataType": "int",
"Length": 0,
"AllowNull": true,
"Format": "",
"IsReadOnly": true,
"IsDateOnly": null
}
],
Rows:[
0
]
}
我期待寫一個角類,它擴展響應,將有一個名爲JsonMinimal特殊的方法,它能夠理解這些數據,並返回一個對象爲了我。
import { Response } from "@angular/http";
export class ServerSource
{
SourceName: string;
MoreItems: boolean;
Error: string;
ExtendedProperties: ExtendedProperty[];
Columns: Column[];
}
export class ServerSourceResponse extends Response
{
JsonMinimal() : any
{
return null; //Something that will be a blank any type that when returned I can perform `object as FinalObject` syntax
}
}
我知道StackOverflow上不是要求對問題的完整的解決方案,所以我只問什麼就是一例服用此示例數據和創建打字稿不會在我喊動態響應。我不知道該做什麼,這個開發人員有成千上萬的服務器端方法,並且都以JSON或XML輸出的形式返回字符串。我基本上正在尋找一種方法來獲取他的列數據,並將其與適當的行數據結合起來,然後有一個更大的對象來保存這些組合對象。
這個數據映射到一個基本對象後的用例就是這樣的。 例子:
var data = result.JsonMinimal() as LoginResponse; <-- Which will map to this object correctly if all the data is there in a base object.
var pk = data.ClientPK.Value;
你爲什麼要做'var finalObject = Object.assign({},rawObject);'?.另外,什麼是「LoginResponse」?我想你可能在這裏過於複雜,但我不清楚你想達到什麼目的。 – Hinrich
我還沒有真正開始使用任何有用的代碼,我將它從代碼示例中刪除。 –
請注意,名爲'JsonMinimal()'的函數不返回JSON,而是返回一個對象。 JSON是一種字符串格式。 –