Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 循环JSON数组[10]_Arrays_Json_Angular_Typescript - Fatal编程技术网

Arrays 循环JSON数组[10]

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: &

我是Angular10的新手,我正在尝试循环一个JSON对象 但我无法解决

我的JSON对象:

[
{
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说的做的(你也这么做了),我解决了它,感谢它的帮助:)