Angular 模型中字段的 HttpClient SerializedName
Angular HttpClient SerializedName of fields in model
有没有办法在模型class字段中添加一些attribute/annotation来标记字段的序列化名称(问题基于@SerializedName
来自Gson
) json 来自服务器的响应?
例如来自服务器的响应包含 start_date
或 some_date
之类的字段,我希望我的模型中的字段名为 startDate
或 acquisitionDate
。在我的 Spring 中,响应模型中的服务器字段被注释为 @SerializedName("start_date")
。
我这样使用 Angular 10
和 HttpClient
:
httpClient.get<MyModel[]>(environment.apiUrl + '/getData')
.pipe(map(models => models.map(m => Object.assign(new MyModel(), m)));
我找到并使用了库 https://github.com/typestack/class-transformer
另外,如果有人需要,可以使用类似的库 https://github.com/weichx/cerialize and https://github.com/kmathy/cerializr
有没有办法在模型class字段中添加一些attribute/annotation来标记字段的序列化名称(问题基于@SerializedName
来自Gson
) json 来自服务器的响应?
例如来自服务器的响应包含 start_date
或 some_date
之类的字段,我希望我的模型中的字段名为 startDate
或 acquisitionDate
。在我的 Spring 中,响应模型中的服务器字段被注释为 @SerializedName("start_date")
。
我这样使用 Angular 10
和 HttpClient
:
httpClient.get<MyModel[]>(environment.apiUrl + '/getData')
.pipe(map(models => models.map(m => Object.assign(new MyModel(), m)));
我找到并使用了库 https://github.com/typestack/class-transformer 另外,如果有人需要,可以使用类似的库 https://github.com/weichx/cerialize and https://github.com/kmathy/cerializr