2016-09-27 4 views
0

THISページで、私はダミーエステートをいくつかリストし、クリックごとに電話番号を明らかにするスパン(テキスト「VEZI TELEFON」)をハードコードしました。jsonファイルのデータでspanタグを設定する

:以下見ることができます私のコードは、出力望ましい結果をしません

{ 
    "telefoane":[{ 
    "id":"1", 
    "tel":"0743127315" 
}, 
{ 
    "id":"2", 
    "tel":"072245875" 
}, 
{ 
    "id":"3", 
    "tel":"0756129458" 
}, 
{ 
    "id":"4", 
    "tel":"0725127216" 
}, 
{ 
    "id":"5", 
    "tel":"0723127322" 
}] 
} 

:私は、JSON(またはPHP)から取得するためにあらゆる電話番号ファイルは、コンテンツを持っているtelefoane.jsonと呼ばれたいです

$.ajax({ 
url: 'telefoane.json', 
dataType: 'json', 
success: function(data){ 
    $.each(data, function(key, val){ 
    console.log(key + ":" + val); 
    }); 
} 
}); 

出力は、残念ながら、次のとおりです。私は間違って何をやっている

telefoane:[object Object],[object Object],[object Object],[object Object],[object Object] 

?ありがとうございました!

EDIT:

$.ajax({ 
    url: 'telefoane.json', 
    dataType: 'json', 
    success: function(){ 
     $.each(data.telefoane, function() { 
      console.log(this.id + ": " + this.tel); 
     }); 
    } 
}); 

答えて

2

あなたのループはあなたにdata.telefoaneのオブジェクトを提供します。したがって、プロパティ名でコンテンツにアクセスする必要があります。

var data = { 
 
    "telefoane": [ 
 
    { 
 
    "id": "1", 
 
    "tel": "0743127315" 
 
    }, { 
 
    "id": "2", 
 
    "tel": "072245875" 
 
    }, { 
 
    "id": "3", 
 
    "tel": "0756129458" 
 
    }] 
 
}; 
 

 
$.each(data.telefoane, function(i, object) { 
 
    console.log(object.id + ": " + object.tel); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

またはthisを使用します。

var data = { 
 
    "telefoane": [ 
 
    { 
 
    "id": "1", 
 
    "tel": "0743127315" 
 
    }, { 
 
    "id": "2", 
 
    "tel": "072245875" 
 
    }, { 
 
    "id": "3", 
 
    "tel": "0756129458" 
 
    }] 
 
}; 
 

 
$.each(data.telefoane, function() { 
 
    console.log(this.id + ": " + this.tel); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

私はあなたのコードを使用している(魔女がobsequiously作品)鉱山内部(成功の後)と、コンソールにはありません何かを出力してください... – user1647234

+0

あなたのコードを表示してください(* maybe as editあなたの質問で*)。 @ user1647234 – eisbehr

+0

Question編集済み – user1647234

関連する問題