简体   繁体   English

如何在JSON字符串中选择两个测试对象并将它们映射到我的界面?

[英]How can I select both test objects in my JSON string and map them to my interface?

Question

How can I select both test objects in my JSON string and map them to my interface called Test ? 如何在JSON字符串中选择两个测试对象并将它们映射到名为Test接口? And is it possible that my property in the interface isnt called some_name ? 而且接口中的我的属性可能不是some_name吗?

Problem 问题

Im trying to map my JSON objects to the Test interface, but it does not work. 我试图将我的JSON对象映射到Test接口,但是它不起作用。 The getTests in the component always returns undefined for 组件中的getTests始终为以下对象返回未定义

I guess the reason is that there is a parent named test , but res.json().data should return only the both objects test 1 and test 2 . 我猜这是因为有一个名为test的父对象,但是res.json()。data应该只返回两个对象test 1test 2

JSON JSON格式

{"test":[{"some_name":"test 1"},{"some_name":"test 2"}]} 

Service Method 服务方式

getTests() {
    return this.http.get(this.baseUrl + "/something")
        .map(res => <Array<Test>> res.json().data)
        .catch(this.handleError);
}

Component 零件

this.myService.getTests()
    .subscribe(
        tests => this.tests = tests,
        error =>  this.errorMessage = <any>error);

From what I see there is no data property in the JSON content you receive. 从我看到的内容来看,您收到的JSON内容中没有data属性。

res.json() returns an object containing a test property which is an array. res.json()返回包含test属性的对象,该属性是一个数组。 You should use .map(res => <Array<Test>> res.json().test) instead to get the array. 您应该使用.map(res => <Array<Test>> res.json().test)来获取数组。

Thierry 蒂埃里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Angular中,如何在我的http get返回结果对象之前对其进行操作? - In Angular, how can I manipulate my result objects before my http get returns them? 如何通过Selenium自动化测试我的json Web服务? - How can i make automation test my json webservices by selenium? 当我在 concat 或 group_concat 中使用 select 并且我想以 JSON 的形式返回它们时,我的查询很慢。 我怎样才能让它更快? - My query is slow when ever I select inside concat or group_concat and I want to return them in form of JSON. how can I make it faster? 如何将JSON字符串转换为JavaScript Object Literal - How can I convert my JSON string to a JavaScript Object Literal 如何将Json对象转换为字符串? - How can I convert my Json object to a string? 如何将json字符串转换为特定格式? - how can I convert my json string to a specific format? 如何使用CSS和JS编写的多选代码仅选择一个选项? 目前,我可以同时选择 - How do I make my multiple choice code written in CSS & JS to only select one option? Currently I can select both Nodejs 如何添加另一个主 app.js 并使用它们来使我的代码干净? - Nodejs how can I add another main app.js and use both of them to make my code clean? 如何根据我的选择标签中选择的数字过滤我的json数据 - How can i filter my json data by the numbers selected in my select tags 如何获取 json 的纬度和经度属性,然后将它们转换为数组? - How can I get the latitude and longitude properties of my json and then transform them into an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM