简体   繁体   English

在 Angular2 中提取 JSON 数据

[英]extract JSON data in Angular2

I have the following JSON output: http://pastebin.com/9bHaBrbX我有以下 JSON 输出: http : //pastebin.com/9bHaBrbX

I would like to make this pseudocode a reality:我想让这个伪代码成为现实:

For every Model {
     For every Year {
         For every Style {
           if submodel.body is NOT in Array:
              load submodel.body to Array;
         }
     }
}

I would like to store this Array in my model.component so I can use it to make buttons such as:我想将此数组存储在我的 model.component 中,以便我可以使用它来制作按钮,例如:

<button *ngFor="let item in Array" ...> item </button>

How can I extract the data I want (Model[x].Years[y].Style[z].submodel.body) (where x,y,z are variables) from my json response?如何从我的 json 响应中提取我想要的数据 (Model[x].Years[y].Style[z].submodel.body)(其中 x,y,z 是变量)?

I am having trouble conceptualizing how to iterate through nested json objects with JS/TS/Angular2.我在概念化如何使用 JS/TS/Angular2 遍历嵌套的 json 对象时遇到了麻烦。

Here is a hint :这是一个提示:

<div *ngFor="let years of model.years">
 <div *ngFor="let year of years">
  <div *ngFor="let styles of year.styles">
   <div *ngFor="let style of styles">
    <!-- your conditions, submodel.body... -->
   </div>
  </div>
 </div>
</div>

Side note: I agree with others above, you should try to write your code yourself.旁注:我同意上面的其他人,您应该尝试自己编写代码。

Use simple http request, it will download the data and store it inside the items variable.使用简单的http请求,它将下载数据并将其存储在items变量中。 Then you can do whatever you want with it - display, sort or just keep.然后你可以用它做任何你想做的事情——显示、排序或只是保留。

https://plnkr.co/edit/bU7W6f0aO6eelvyCnKzc?p=preview https://plnkr.co/edit/bU7W6f0aO6eelvyCnKzc?p=preview

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM