Map Converted XML to JSON

I’m retrieving XML from this forexfactory.net and converting it to JSON using X2JS. I’m able to achieve this on my provider, however I can’t use the data on my page since I can’t map my response to json on the provider “res.json()”.

/** The Provider function*/

getNews(){
    let x2js = new X2JS();
    return this.http.get('xml url').map(res =>{
      let xml = res.text();
      return x2js.xml2js(xml);
    });
  }

/** The page */


export class NewsPage {
  news: any[];

  constructor(private newsProv: NewsProvider) { }

  ionViewDidLoad() {
    this.newsProv.getNews().subscribe(res =>{
      this.news = res;
    }, (err) =>{
      console.log(err);
    });
  }
}

On the line ‘this.news = res;’ I get an error saying “Type {} is not assignable to type ‘any[]’”. And I understand from this issue that its because I didn’t map my response to json “res.json()”. And I’ve tried to on my provider, but I get an error.

What’s the return type of x2js.xml2js? It seems be thinking that it’s an object, not an array as you have it typed up.

1 Like

Perfect!! Yes, it was an object named “weeklyevents” and within it was an “event” Array. And so I changed my “page” function to the code below, and it works like a charm…

ionViewDidLoad() {
    this.newsProv.getNews().subscribe(res =>{
      this.data = res;
      this.news = this.data.weeklyevents.event;
      console.log(this.news)
    }, (err) =>{
      console.log(err);
    });
  }

Thanks dude :sunglasses:

1 Like