Angular HttpClient get方法返回属性名称错误的对象

时间:2019-05-26 18:08:49

标签: angular angular-httpclient

我有一个使用GET方法Products的Web控制器。此方法返回类型为Enumerable的{​​{1}}。

Product

我创建了一个Angular组件,该组件读取控制器方法提供的数据,如下所示:

        public class Product
        {
            public float Price;
            public string Name;
            public string ImageURL;
            public int Quantity;

            public Product()
            {
                this.Quantity = 1;
            }
        }

我的问题是在Angular侧对象中的属性名称被错误地命名,例如:constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) { this.grandTotal = 0; this.httpClient = http; this.url = baseUrl; http.get<Product[]>(baseUrl + 'api/SampleData/Products').subscribe(result => { this.products = result; this.slicedProducts = new Array<Product[]>(); for (let i = 0; i < this.products.length / 3; i++) { this.slicedProducts[i] = this.products.slice(i * 3, i * 3 + 3); } }, error => console.error(error)); } Product.Quantity,因此当我稍后在函数中尝试使用这些属性时,错误Product.quantity不存在。当我使用Quantity时,代码有效。

1 个答案:

答案 0 :(得分:0)

如果您正在使用Java。检查您的设置,以实现从Java对象到Json对象的对象转换。假设您在后端使用Spring Boot,则可能需要配置Jackson来保留字段名称。我假设默认情况下将字段转换为Camel case