2016-01-20 115 views
-1

我有這個JSON數據。但我似乎不能生態職位數組。它的價值是我想要追加的。javascript;循環JSON值不會工作

{ 
    "count": 2, 
    "status": "ok", 
    "count_total": 2, 
    "pages": 1, 
    "posts": [ 
    { 
     "id": 4, 
     "type": "post", 
     "slug": "nana-akufo-addo-visits-pentecost-convention-centre", 
     "url": "http://akufoaddo2016.com/2016/01/17/nana-akufo-addo-visits-pentecost-convention-centre/", 
     "status": "publish", 
     "title": "NANA AKUFO – ADDO VISITS PENTECOST CONVENTION CENTRE", 
     "title_plain": "NANA AKUFO – ADDO VISITS PENTECOST CONVENTION CENTRE", 
     "content": "<p>The Presidential Candidate of the New Patriotic Party (NPP) for the 2016 elections, Nana Addo Dankwa Akufo-Addo, on Friday, January 15, 2016 made a historic visit to the ultra-modern Pentecost Convention Centre (PCC) located at Gomoa Fetteh, near Kasoa. <a href=\"http://akufoaddo2016.com/2016/01/17/nana-akufo-addo-visits-pentecost-convention-centre/#more-4\" class=\"more-link\">Read more</a></p>\n", 
     "excerpt": "<p>The Presidential Candidate of the New Patriotic Party (NPP) for the 2016 elections, Nana Addo Dankwa Akufo-Addo, on Friday, January 15, 2016 made a historic visit to the ultra-modern Pentecost Convention Centre (PCC) located at Gomoa Fetteh, near Kasoa.</p>\n", 
     "date": "2016-01-17 23:56:42", 
     "modified": "2016-01-17 23:56:42", 
     "categories": [ 
     { 
      "id": 2, 
      "slug": "blog", 
      "title": "Blog", 
      "description": "", 
      "parent": 0, 
      "post_count": 1 
     } 
     ], 
     "tags": [], 
     "author": { 
     "id": 1, 
     "slug": "admin", 
     "name": "admin", 
     "first_name": "", 
     "last_name": "", 
     "nickname": "admin", 
     "url": "", 
     "description": "" 
     }, 
     "comments": [], 
     "attachments": [ 
     { 
      "id": 5, 
      "url": "http://akufoaddo2016.com/wp-content/uploads/2016/01/akufoaddo-pentecost-jan16.jpg", 
      "slug": "akufoaddo-pentecost-jan16", 
      "title": "akufoaddo-pentecost-jan16", 
      "description": "", 
      "caption": "", 
      "parent": 4, 
      "mime_type": "image/jpeg", 
      "images": { 
      "full": { 
       "url": "http://akufoaddo2016.com/wp-content/uploads/2016/01/akufoaddo-pentecost-jan16.jpg", 
       "width": 430, 
       "height": 334 
      }, 
      "thumbnail": { 
       "url": "http://akufoaddo2016.com/wp-content/uploads/2016/01/akufoaddo-pentecost-jan16-150x150.jpg", 
       "width": 150, 
       "height": 150 
      }, 
      "medium": { 
       "url": "http://akufoaddo2016.com/wp-content/uploads/2016/01/akufoaddo-pentecost-jan16-300x233.jpg", 
       "width": 300, 
       "height": 233 
      }, 
      "medium_large": { 
       "url": "http://akufoaddo2016.com/wp-content/uploads/2016/01/akufoaddo-pentecost-jan16.jpg", 
       "width": 430, 
       "height": 334 
      }, 
      "large": { 
       "url": "http://akufoaddo2016.com/wp-content/uploads/2016/01/akufoaddo-pentecost-jan16.jpg", 
       "width": 430, 
       "height": 334 
      }, 
      "post-thumbnail": { 
       "url": "http://akufoaddo2016.com/wp-content/uploads/2016/01/akufoaddo-pentecost-jan16.jpg", 
       "width": 430, 
       "height": 334 
      } 
      } 
     } 
     ], 
     "comment_count": 0, 
     "comment_status": "open", 
     "thumbnail": "http://akufoaddo2016.com/wp-content/uploads/2016/01/akufoaddo-pentecost-jan16.jpg", 
     "custom_fields": {}, 
     "thumbnail_size": "post-thumbnail", 
     "thumbnail_images": { 
     "full": { 
      "url": "http://akufoaddo2016.com/wp-content/uploads/2016/01/akufoaddo-pentecost-jan16.jpg", 
      "width": 430, 
      "height": 334 
     }, 
     "thumbnail": { 
      "url": "http://akufoaddo2016.com/wp-content/uploads/2016/01/akufoaddo-pentecost-jan16-150x150.jpg", 
      "width": 150, 
      "height": 150 
     }, 
     "medium": { 
      "url": "http://akufoaddo2016.com/wp-content/uploads/2016/01/akufoaddo-pentecost-jan16-300x233.jpg", 
      "width": 300, 
      "height": 233 
     }, 
     "medium_large": { 
      "url": "http://akufoaddo2016.com/wp-content/uploads/2016/01/akufoaddo-pentecost-jan16.jpg", 
      "width": 430, 
      "height": 334 
     }, 
     "large": { 
      "url": "http://akufoaddo2016.com/wp-content/uploads/2016/01/akufoaddo-pentecost-jan16.jpg", 
      "width": 430, 
      "height": 334 
     }, 
     "post-thumbnail": { 
      "url": "http://akufoaddo2016.com/wp-content/uploads/2016/01/akufoaddo-pentecost-jan16.jpg", 
      "width": 430, 
      "height": 334 
     } 
     } 
    }, 
    { 
     "id": 1, 
     "type": "post", 
     "slug": "hello-world", 
     "url": "http://akufoaddo2016.com/2016/01/13/hello-world/", 
     "status": "publish", 
     "title": "Hello world!", 
     "title_plain": "Hello world!", 
     "content": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!</p>\n", 
     "excerpt": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!</p>\n", 
     "date": "2016-01-13 21:24:36", 
     "modified": "2016-01-13 21:24:36", 
     "categories": [], 
     "tags": [], 
     "author": { 
     "id": 1, 
     "slug": "admin", 
     "name": "admin", 
     "first_name": "", 
     "last_name": "", 
     "nickname": "admin", 
     "url": "", 
     "description": "" 
     }, 
     "comments": [ 
     { 
      "id": 1, 
      "name": "Mr WordPress", 
      "url": "https://wordpress.org/", 
      "date": "2016-01-13 21:24:36", 
      "content": "<p>Hi, this is a comment.<br />\nTo delete a comment, just log in and view the post&#039;s comments. There you will have the option to edit or delete them.</p>\n", 
      "parent": 0 
     } 
     ], 
     "attachments": [], 
     "comment_count": 1, 
     "comment_status": "open", 
     "custom_fields": {} 
    } 
    ] 
} 

我試圖消耗下面的JavaScript代碼的值,但它不是工作

$(document).ready(function() { 
    $.ajax({ 
    cache: false, 
     url: "(url of my api)", 
    type: 'GET', 
    crossDomain: true, 
    dataType: 'json', 
    success: function() { 
     alert("Success"); 
    }, 
    error: function() { 
     alert('Failed!'); 
    }, 
}).then(function(data) { 
    for (var i = 0; i < post.length; i++) 
    { 
     var prefix = (i == 0 ? "" : i.toString()); 
     $('.title' + prefix).append(data[i].posts[title]); 
     $('.content' + prefix).append(data[i].posts[content]); 

    } 
}); 

}); 

謝謝大家提前。

+0

你已經試過了什麼? –

回答

3

由於數據是一個對象,帖子是你需要得到的職位爲data.posts

for (var i = 0; i < data.posts.length; i++) 
    { 
     var prefix = (i == 0 ? "" : i.toString()); 
     $('.title' + prefix).append(data.posts[i]["title"]); 
     $('.content' + prefix).append(data.posts[i]["content"]); 
    } 
1

then部分之前for迴路中加入這一行

var post = data.posts; 

post變量沒有從data響應JSON值初始化。

1

希望對象內部的數組這個片段將是有益的

var someVar = // your json 
var getPostArray = a.posts; // Will give you the posts key and its value 

DEMO

0

@Nighishacorrect。在另一方面,你可以簡化你的代碼...

$(document).ready(function(){ 

// Enclose into a function, can be reused and managed easily .... 
function loadPosts(){ 
    $.ajax({ 
     url: '', 
     type: 'GET', 
     crossDomain: true, 
     dataType: 'json', 
     cache: false 
    }).then(function(data){ 
     // use the same variable, instead of setting into another (eg: post = data.posts) .. 
     data.posts.forEach(function(elm, idx){ 
      var prefix = (idx == 0 ? '': idx); // toSting() is not required .... 
      $('.title' + prefix).append(elm['title']); 
      $('.content' + prefix).append(elm['content']); 
     }); 
    }); 
} 

// invoking function finally :P ... 
loadPosts(); 

});