2016-11-22 14 views
0

このJSONファイルの配列をテーブルにリストするにはどうすればよいですか? titleのような単一の値を見つけることができますが、私はresult.ingredients [index]を呼び出してみます。定義されていませんか?試しましたresult.recipe.ingredients [index]あまりにもまだ未定義と私はこれを行う方法について見つけることができるほとんどのオンライン?JSONからブートストラップテーブルへの配列の割り当て

var result = []; 


$.ajax({ 
    url: 'https://community-food2fork.p.mashape.com/get?key=0ee5a01caf7f7c3512b54978628f1a4e&rId=37859', // The URL to the API. You can get this in the API page of the API you intend to consume 
    type: 'GET', // The HTTP Method, can be GET POST PUT DELETE etc 
    data: {}, // Additional parameters here 
    dataType: 'json', 
    success: function(data) { 
     result = JSON.parse(JSON.stringify(data.recipe)); 
     console.dir(result.ingredients); 
     $('#location').append($.map(result, function (ignore, index) { 
      return '<tr><td>' + result.title+ '</td><td>' + result.ingredients[index] + '</td></tr>'; 
     }).join()); 
    }, 
    error: function(err) { alert(err); }, 
    beforeSend: function(xhr) { 
     xhr.setRequestHeader("X-Mashape-Authorization", "KEY"); // Enter here your Mashape key 
    } 
}); 

JSON:

Object 
f2f_url: 
"http://food2fork.com/view/37859" 
image_url: 
"http://static.food2fork.com/chickenturnover2_300e6667e66.jpg" 
ingredients: 
Array[6] 
0: 
"1 1/2 cups shredded rotisserie chicken" 
1: 
"1 1/2 cups grated Gruyre" 
2: 
"1 cup frozen peas" 
3: 
"2 sheets (one 17.25-ounce package) frozen puff pastry, thawed" 
4: 
"1 large egg, beaten" 
5: 
"1/4 cup Dijon mustard↵" 
length: 
6 
__proto__ 
: 
Array[0] 
publisher 
: 
"Real Simple" 
publisher_url 
: 
"http://realsimple.com" 
recipe_id 
: 
"37859" 
social_rank 
: 
99.84842829206659 
source_url 
: 
"http://www.realsimple.com/food-recipes/browse-all-recipes/chicken-and-gruyere-turnovers-00000000052482/index.html" 
title 
: 
"Chicken and Gruyre Turnovers" 
+0

jsonの例を示してください、URLが機能していません。 – Kenji

+0

投稿にそれを加えました –

+0

@ConorT元の投稿に編集としてコードサンプルを含めることをお勧めします – demongolem

答えて

0

あなたが食材を反復処理し、それらをリストアップしようとしている場合、あなたは簡単な各ループ

EDITが必要な場合があります。各

success: function(result) {       
    var msg; 
    $.each(result.ingredients, function (key, ingredient) { 
     msg = msg+ '<tr><td>' + result.title+ '</td><td>' + ingredient + '</td></tr>'; 
    })); 
    $('#location').append(msg);  
} 
を使用し

地図を使用

success: function(result) {       
    var msg = 
    $.map(result.ingredients, function (ingredient, index) { 
     return '<tr><td>' + result.title+ '</td><td>' + ingredient + '</td></tr>'; 
    }).join(); 
    $('#location').append(msg);  
} 
+0

パーフェクト、ありがとう、私はそれを複雑にしていたと思う。 –

+0

私は次のようにマップに合わせてMYNを編集:$( '#場所を')( $ .MAP(result.ingredients、機能(インデックス、無視する){ リターン '​​' + result.title + '​​を追加'+ result.ingredients [index] +' '; })。join() –

関連する問題