2016-11-01 9 views
-1

私はHttp getをAngular 2で行い、レスポンスはJSONです。しかし、私はこれをngForで使用しようとしていますが、配列ではないのでできません。JSON HttpレスポンスをAngularJSのArrayに変換する方法

JSONを角度2の配列に変換するにはどうすればよいですか?私は多くのウェブサイトで検索しましたが、効果的な方法を発見しませんでした。

編集1:

応答がそのようなものです:

{ 
    "adult": false, 
    "backdrop_path": "/fCayJrkfRaCRCTh8GqN30f8oyQF.jpg", 
    "belongs_to_collection": null, 
    "budget": 63000000, 
    "genres": [ 
    { 
     "id": 18, 
     "name": "Drama" 
    } 
    ], 
    "homepage": "", 
    "id": 550, 
    "imdb_id": "tt0137523", 
    "original_language": "en", 
    "original_title": "Fight Club", 
    "overview": "A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground \"fight clubs\" forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.", 
    "popularity": 0.5, 
    "poster_path": null, 
    "production_companies": [ 
    { 
     "name": "20th Century Fox", 
     "id": 25 
    } 
    ], 
    "production_countries": [ 
    { 
     "iso_3166_1": "US", 
     "name": "United States of America" 
    } 
    ], 
    "release_date": "1999-10-12", 
    "revenue": 100853753, 
    "runtime": 139, 
    "spoken_languages": [ 
    { 
     "iso_639_1": "en", 
     "name": "English" 
    } 
    ], 
    "status": "Released", 
    "tagline": "How much can you know about yourself if you've never been in a fight?", 
    "title": "Fight Club", 
    "video": false, 
    "vote_average": 7.8, 
    "vote_count": 3439 
} 
+0

応答の 'console.log'を行い、それを質問に追加してください。 – AndreaM16

+0

@ AndreaM16編集1をチェックしてください。 –

+0

@KodieGrantham私はAngularJS 2を使用していますが、この質問はAngularJS 1に基づいています。 –

答えて

0

私はあなたが配列にJSONから渡したい場合は、以下のコマンドを実行することができると思う:

var arr = [] 
for(i in json_object){ 
    arr.push(i) 
    arr.push(json_object[i]) 
} 

次に、偶数インデックスのすべてのキーと奇数インデックスのすべてのコンテンツを持ちます

0

まあ、私は本当にここにポイントが表示されません。配列は、複雑な構造ではなく、類似のオブジェクトまたは型のリストを操作するための配列です。あなたが表示するオブジェクトに似たたくさんのオブジェクトを持っていたら、それは理にかなっています。とにかく、本当に配列が必要な場合は、再帰でそれを行い、プロパティのフラットな配列を作成することができます。

var flatPropertyArray = []; 
function flatten(obj) { 
    for (var property in obj) { 
     if (obj.hasOwnProperty(property)) { 
      if (typeof obj[property] == "object") 
       flatten(obj[property]); 
      else 
       flatPropertyArray.push(property); 
     } 
    } 
} 

フラット化のFUNCにあなたのJSONを渡します。

関連する問題