问题描述
我正在尝试使用 Angular 11.2.3 在 HTML 页面上显示 XML 文档。我正在使用 xml2js 解析器(下面的代码),并试图将响应转换为可用的 JSON 对象以在生成的 HTML 页面中循环 *ngFor
但我收到错误:
***Cannot set property 'objJSON' of undefined***
xml2js 解析器代码:
export class ExploreContentComponent implements OnInit {
feed: RssFeed = initialFeed;
stringJSON: any;
objJSON: any;
constructor(private http: HttpClient) { }
ngOnInit(): void {
this.http.get(
'https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml',{
headers: new HttpHeaders(),responseType: 'text'
}
).subscribe(posts => {
xml2js.parseString(posts,function (err,result) {
console.log("rssFeed",result);
console.log("RESULT TORN UP",result.rss.channel[0].item);
this.objJSON = result.rss.channel[0].item;
console.log("RESULT TYPE",typeof(result.rss.channel[0].item));
});
});
}
}
输出:
RESULT (typeof) 作为 OBJECT 返回
结果被撕毁:
在 Chrome-Console 中显示了一个有效的数组对象...很多元素和键/值。
我尝试过的:
const json = JSON.stringify(result.rss.channel[0].item); <<-this will convert result to a string.
console.log("STRINGIED JSON",json); <<-this shows that valid string no problem
this.objJSON = JSON.parse(json); <<- Still get same error :(
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)