Arrays 循环JSON数组[10]
我是Angular10的新手,我正在尝试循环一个JSON对象 但我无法解决 我的JSON对象:Arrays 循环JSON数组[10],arrays,json,angular,typescript,Arrays,Json,Angular,Typescript,我是Angular10的新手,我正在尝试循环一个JSON对象 但我无法解决 我的JSON对象: [ { ID: "2", testata: "Corriere.it - Homepage", url: "http://xml2.corriereobjects.it/rss/homepage.xml", categoria: "QUOTIDIANI" }, { ID: "3", testata: &
[
{
ID: "2",
testata: "Corriere.it - Homepage",
url: "http://xml2.corriereobjects.it/rss/homepage.xml",
categoria: "QUOTIDIANI"
},
{
ID: "3",
testata: "Open",
url: "https://www.open.online/feed/",
categoria: "NEWS"
},
{
ID: "4",
testata: "ANSA.it",
url: "https://www.ansa.it/sito/ansait_rss.xml",
categoria: "NEWS"
}
]
this.httpClient.get('http://localhost/myjson.json').subscribe(data => {
datax.forEach(element => {
console.log(element.url);
});
我的角度脚本的一部分:
[
{
ID: "2",
testata: "Corriere.it - Homepage",
url: "http://xml2.corriereobjects.it/rss/homepage.xml",
categoria: "QUOTIDIANI"
},
{
ID: "3",
testata: "Open",
url: "https://www.open.online/feed/",
categoria: "NEWS"
},
{
ID: "4",
testata: "ANSA.it",
url: "https://www.ansa.it/sito/ansait_rss.xml",
categoria: "NEWS"
}
]
this.httpClient.get('http://localhost/myjson.json').subscribe(data => {
datax.forEach(element => {
console.log(element.url);
});
错误
error TS2339: Property 'forEach' does not exist on type 'Object'.
datay.forEach(element => {
我认为您的问题在于您如何声明响应,您称之为“数据”,但当您尝试使用它时,您称之为“datax”请更改您的代码,如下所示,作为临时修复
this.httpClient.get('http://localhost/myjson.json').subscribe((data: any)=> {
data.forEach(element => {
console.log(element.url);
});
});
默认情况下,Typescript需要响应的类型。如果未提供,则假定为对象
在旁注中,建议不要将值键入
any
。请正确键入值。订阅后是否尝试记录数据中的内容?我想它可能是一个具有response
属性的对象。另一个问题是,如果您在浏览器中输入该URL,您是否能够获取该.json
文件?谢谢Michal,现在我通过定义数组“any”来解决错误。您说得对,但我只是在这里复制了错误的代码,数组总是“data”,所以问题仍然存在?如果它真的这样做了:我没有写data=>{}
write(data:any)=>{}
,事实上我是按照adhs91说的做的(你也这么做了),我解决了它,感谢它的帮助:)