2017-03-01 14 views
1

jsonとjqueryを使用して投稿のリストを表示しようとしています。Jsonオブジェクト配列の値を取得する方法は?

これは、私がデータ

var app = { 

    init: function() { 
     app.getPosts(); 
    }, 

    getPosts: function() { 

     var rootURL = 'https://www.example.com/wp-json/wp/v2/posts'; 

     $.ajax({ 
      type: 'GET', 
      url: rootURL, 
      dataType: 'json', 
      success: function(data){ 

       $.each(data, function(index, value) { 
        console.log(value.featured_image); 
        $('ul.topcoat-list').append('<li class="topcoat-list__item">' + 
        '<h3>'+value.title+'</h3>' + 
        '<p>'+value.excerpt+'</p></li>'); 
        ; 
       }); 
      }, 
      error: function(error){ 
       console.log(error); 
      } 

     }); 

    } 

} 

を取得するために使用していますものですが、このコードは、単に output

を以下返しますが、私はhttps://www.example.com/wp-json/wp/v2/postsに行くとき、私は完璧な配列を参照してください。

アレイの例が

{ 
    "id":2267, 
    "type":"post", 
    "title":{"rendered":"When to visit Arugam Bay? &#8211; Arugam Bay Weather &#038; Seasons \u2013"}, 
    "content":{"rendered":"<div class=\"circle-headline\">\n<p>The right answer is<\/p>\n<\/div>\n<p class=\"wpk-circle-title text-custom\">ALL YEAR LONG!<\/p>\n<p>You can visit Arugam Bay anytime and always find amazing sunny weather. Despite this incredible where, there is a high and low season.<\/p>\n","protected":false}, 
    "excerpt":{"rendered":"<p>The right answer is<\/p>\n<p>ALL YEAR LONG!<br \/>\nYou can visit Arugam Bay anytime and always find amazing sunny weather. Despite this incredible where, there is a high and low season.<\/p>\n","protected":false}, 
    "author":1, 
    "featured_media":2268, 
    "comment_status":"open", 
    "ping_status":"open", 
    "sticky":false, 
    "template":"", 
    "format":"standard", 
} 

は、どのように私はそこからタイトルや抜粋を取得し、それを使用してリストを表示することができますか?

+0

が...これはフラットなおっぱいを持っていることと思いますなぜ..なぜあなたはそれが好きではないのですか? –

+0

ダウンボートするのは私ではありませんが、単純なデバッグロジックを書くことで、あなたの質問を簡単に解決できると思います。結果をデバッグするために 'console.log()'関数をもっと活用してください。 – Raptor

答えて

4

アクセス実際の文字列が含まれレンダリングされたプロパティ:誰もが、少なくとも私に知らせdownvoteしたい場合は任意の説明なしで5分以内の票降りて2

'<h3>'+value.title.rendered+'</h3>' + 
'<p>'+value.excerpt.rendered+'</p></li>'); 
... 
+0

が正しいようです。 +1 –

+0

すてきな努力... + 1 –

関連する問題