使用 Rxjs 映射函数时如何保留对象 属性 大小写
How to preserve object property casing when using Rxjs map function
我是 Angular2 的新手,使用 http 服务从 asp.net mvc webapi 获取数据。
API returns 格式如下所示的数据
RequestResponse{
public string Message {get;set;}
public int Status {get;set;}
}
Http get 请求获取数据并从 Rxjs
map(result => result.json())
馈送到 map
函数,它将结果转换为如下所示的格式
{
message : 'success!',
status : 1
}
如您所见,输出 属性 名称已不区分大小写。
从其他 SO 问题中了解到我们必须编写自己的映射器函数以保持区分大小写。
我的疑问是,是否可以从 Rxjs
配置映射器函数以保持区分大小写?还是我们应该编写自己的映射器函数?
编辑: 问题似乎来自 .json()
(?) 为什么 .json()
将 属性 名称转换为小写?
编辑: 解决方案: 感谢@Sergey . The issue is from .Net Core which was by default converting property names to camelCase. If anybody searcing for the solution, please visit Github page.
Map 是一个简单的函数,可让您转换数据。它非常适合您的需求:
request.map(response => {
let data = response.json();
return <RequestResponse> {
Message: data.message,
Status: data.status
};
});
我是 Angular2 的新手,使用 http 服务从 asp.net mvc webapi 获取数据。
API returns 格式如下所示的数据
RequestResponse{
public string Message {get;set;}
public int Status {get;set;}
}
Http get 请求获取数据并从 Rxjs
map(result => result.json())
馈送到 map
函数,它将结果转换为如下所示的格式
{
message : 'success!',
status : 1
}
如您所见,输出 属性 名称已不区分大小写。
从其他 SO 问题中了解到我们必须编写自己的映射器函数以保持区分大小写。
我的疑问是,是否可以从 Rxjs
配置映射器函数以保持区分大小写?还是我们应该编写自己的映射器函数?
编辑: 问题似乎来自 .json()
(?) 为什么 .json()
将 属性 名称转换为小写?
编辑: 解决方案: 感谢@Sergey
Map 是一个简单的函数,可让您转换数据。它非常适合您的需求:
request.map(response => {
let data = response.json();
return <RequestResponse> {
Message: data.message,
Status: data.status
};
});