简体   繁体   English

我如何将 json 响应 object 的特定数据类型存储在数组中(例如:数字)以将数据源作为变量传递

[英]How can i store the particular data type of the json response object in an array (ex: of numbers) to pass in datasource as variable

export class AppComponent implements OnInit {
    title: String = "Heatmap";
  
    constructor(private app:AppServiceService) { }
    ngOnInit(): void {
        this.app.getdata().subscribe
        (
            (response)=>{console.log(response)}       
        )
    }
   

    dataSource: Object []= [
    [1, this.num.value, 9, 23, 0, 39, 0],
    [0, 18, 37, 0, 0, 50, 0]
    
    ];
    
 
    
   
}

Declare the type somewhere in your app, for example in your service:在您的应用程序中的某处声明类型,例如在您的服务中:

import { Injectable } from "@angular/core";
import { Observable, of } from "rxjs";

export interface GetDataResponse {
    data: number[]
}


@Injectable()
export class AppService {
    public getData(): Observable<GetDataResponse> {
        return of({ data: [1, 2, 3, 4] });
    }
}

Then use it in your subscribe parameter:然后在您的订阅参数中使用它:

ngOnInit() {
    this.appService.getData().subscribe((response: GetDataResponse) => {
      console.log(response);
    })
  }

If the response has a simple type, you can just use it directly:如果响应具有简单类型,则可以直接使用它:

ngOnInit() {
    this.appService.getData().subscribe((response: {data: number[]}) => {
      console.log(response);
    })
}

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

相关问题 如何从JSON响应中删除特定数据 - How can i remove particular data from JSON response 如何将 API 调用响应存储为 object 或将数组解析为 JSON? - How can I store an API call response as an object or parse an array as a JSON? 我如何将数据传递到对象数组中 - how can i pass data into array of object How can i convert the mongoose aggregation array of object of object response into json object response - How can i convert the mongoose aggregation array of object of object response into json object response 如何将json响应存储到变量中? - How to store json response into variable? 使用 API json 响应 - 如何提取嵌套数组数据并将其存储在 JavaScript 数组中? - working with API json response - how do I extract nested array data and store it in a JavaScript array? 如何从 JSON 对象加载数据...而不是 JSON 数组 - How can I load data from JSON Object… not JSON Array 如何呈现特定的JSON数据? - How can i render a particular JSON data? 我如何访问通过 ajax 发送到 php 文件的 POST 请求的 JSON 数据并将它们存储在数组变量中? - how can i acces the JSON data of POST request sent through ajax to a php file and store them in an array variable? 如何将POST响应中的json数据存储在变量中? - How to store json data from a POST response in a variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM