Angular 如何作为get请求的结果获取JSON数组的值?
如何获取JSON数组的值作为获取请求的结果Angular 如何作为get请求的结果获取JSON数组的值?,angular,spring-boot,Angular,Spring Boot,如何获取JSON数组的值作为获取请求的结果 @组件({ 选择器:“应用程序导航栏”, templateUrl:'./navbar.component.html', 样式URL:['./navbar.component.css'] }) 导出类NavbarComponent实现OnInit{ 国家=[“乌克兰”、“南非”]; 数据:数组=新数组(); 构造函数(私有http:HttpClient){} ngOnInit():void{ this.http.get('http://localhost
@组件({
选择器:“应用程序导航栏”,
templateUrl:'./navbar.component.html',
样式URL:['./navbar.component.css']
})
导出类NavbarComponent实现OnInit{
国家=[“乌克兰”、“南非”];
数据:数组=新数组();
构造函数(私有http:HttpClient){}
ngOnInit():void{
this.http.get('http://localhost:8080/countries/db')
.订阅(数据=>{
这个数据=数据;
});
console.log(this.data);
}
}
JSON
我想将JSON的值作为字符串数组存储在数据变量中,使用AngularTry stringify():
我不确定我是否理解:
this.http.get<Array<Country>>('http://localhost:8080/countries/db')
.subscribe(data => {
const countries = data;
const countriesLis: Array<string> = new Array<string>()
for( let country of countries) {
countriesList.push(country);
}
this.data = countriesList;
});
this.http.get('http://localhost:8080/countries/db')
.订阅(数据=>{
const countries=数据;
常量countries:数组=新数组()
for(让国家中的国家){
国家列表推送(国家);
}
this.data=countriesList;
});
真的把你的问题弄糊涂了。您想要JSON对象作为字符串吗?或者别的什么?把控制台放在subscribe中..实际上我想把这个json的值放在javascript对象中。谢谢你的帮助。我之前试过它不工作,typescript给出了一个编译器错误。这是正确的,谢谢你,它工作得很好。
const data= { name: "John", age: 30, city: "New York" };
this.data = JSON.stringify(data);
this.http.get<Array<Country>>('http://localhost:8080/countries/db')
.subscribe(data => {
const countries = data;
const countriesLis: Array<string> = new Array<string>()
for( let country of countries) {
countriesList.push(country);
}
this.data = countriesList;
});