2017-03-23 5 views
0

私は過去3日間この問題を抱えていました。私はXMLをHTTP経由でJSONに変換して、イオン2で表示できるようにしたいと思います。プロバイダのコードはここにあります。どんな助けもありがとう!ありがとう!それはこのxmlをJson ionic 2に解析する方法

'typings' is not recognized as an internal or external command, 
operable program or batch file. 

そして、最初の使用のようなエラーが発生した場合あなたは今、コマンド

typings install xml2json --save 

を使用してxml2jsonためのタイピングをインストールすることができ、イオン2 にJSONにXMLを解析するための

import { Injectable } from '@angular/core'; 
import { Http } from '@angular/http'; 

import 'rxjs/add/operator/map'; 

import * as xml2js from "xml2js"; 


@Injectable() 
export class News { 

    constructor(public http: Http) { } 

    public getData() { 

     this 
      .http 
      .get('https://www.ug.edu.gh/news.xml') 
      .map(res => { 

       var cleanedString = res.toString().replace("\ufeff", ""); 

       xml2js 
       .parseString(cleanedString, (error, result) => { 

        console.log(result); 
        return result; 

       }); 

      }) 
      .subscribe((data) => { 
       console.log(data) 
      }, (err) => { 
       console.log(err) 
      }); 

    } 
} 

答えて

0

入力をインストールするこのコマンド

npm install typings -g 

そして、その後、それが正常に動作します以前のコマンドに

typings install xml2json --save 

を実行します。 あなたのために働くことを願っています

0

あなたはほぼあります。あなたはparseStringメソッドからのデータreturnが間違っています。あなただけの、一時的な変数を使用parseString方法から結果を割り当て、そのようにその一時的な変数を返す必要があります:

// ... 
.map(res => { 

    let cleanedString = res.toString().replace("\ufeff", ""); 
    let jsonData; 

    xml2js 
    .parseString(cleanedString, (error, result) => { 

     if(error) { 
      console.error(error); 
      jsonData = error; 
     } else { 
      jsonData = result; 
     } 

    }); 

    return jsonData; 

}).subscribe((data) => { 
// ... 

より多くの深さの説明に代替方法が与えられているhere.