【发布时间】:2021-01-16 20:52:58
【问题描述】:
我正在使用一个返回 XML 的 api,并且在将其转换为 json 时遇到问题,我尝试使用 xml2js,但我一直在控制台中未定义
这是我的 api 服务
export class WordgameService {
public apiUrl ="http://www.wordgamedictionary.com/api/v1/references/scrabble/";
public apiKey ="********************\";
constructor(private http: HttpClient) {
}
getSearchTerm(inputValue: string){
var xmlString = this.http.get(this.cors+this.apiUrl + inputValue+this.apiKey,{ responseType: 'text' , headers:{'Content-Type': 'application/xml'}}).subscribe(response => {
console.log(response);
});
var json = this.convertToJson(xmlString)
console.log(json)
return json;
console.log(json)
}
convertToJson(xml:any){
var json =xml2js.parseString(xml,function(err,result){
})
}
和组件
export class WordCheckerComponent implements OnInit {
searchword = new FormControl('');
private results :any;
constructor(private apiService: WordgameService) {
}
searchWord() {
this.results = this.apiService.getSearchTerm(this.searchword.value);
}
ngOnInit(): void {
}
}
这里也是对“测试”这个词的回应
<entry>
<word>test</word>
<scrabble>1</scrabble>
<scrabblescore>4</scrabblescore>
<sowpods>1</sowpods>
<sowpodsscore>4</sowpodsscore>
<wwf>1</wwf>
<wwfscore>4</wwfscore>
</entry>
【问题讨论】:
-
您尝试转换响应的代码在哪里......?
-
对不起,我在发帖前把它拿出来了
-
我尝试使用 xml2js 但我一直不确定?
-
你需要把 var json = this.convertToJson(xmlString) console.log(json) 放在订阅里面..
-
iv 刚刚完成,但我得到了一个订阅者对象?,我在问题中发布了一张图片
标签: json angular xml typescript