2

I have a JSON feed I'm working with that contains one instance of attributes and many objects inside posts.

I have a javascript algorithm that I'm processing the JSON data and I am getting the attributes values fine (they act as settings) but I can't process the posts properly.

JSON Response

[
  {
  "attributes":{
     "dfp_pos":"3",
     "dfp_shortcode":"[dfp_ads id=1383]",
     "wildcard_pos":"5",
     "wildcard_shortcode":"[widget id=\"wp_widget-46\"]",
     "twitter_id":"@ScreenName",
     "twitter_pos":"8",
     "facebook_id":"screenName",
     "facebook_pos":"10"
  },
  "posts":[
     {
        "id":3945,
        "pub_date":"2016-05-30 00:00:00",
        "title":"Post title 1",
        "excerpt":""
     },
     {
        "id":3574,
        "pub_date":"2016-05-12 00:00:00",
        "title":"Post Title 2",
        "excerpt":""
     },
     {
        "id":3048,
        "pub_date":"2016-05-07 00:00:00",
        "title":"Post Title 3",
        "excerpt":""
     }
   ]
]

Javascript

var dfp_pos = feeds[0].attributes.dfp_pos;
var dfp_shortcode = feeds[0].attributes.dfp_shortcode;
var wildcard_pos = feeds[0].attributes.wildcard_pos;
var wildcard_shortcode = feeds[0].attributes.wildcard_shortcode;
var twitter_id = feeds[0].attributes.twitter_id;
var twitter_pos = feeds[0].attributes.twitter_pos;
var facebook_id = feeds[0].attributes.facebook_id;
var facebook_pos = feeds[0].attributes.facebook_pos;

jQuery.each(feeds.posts, function(i, post) {
    var id = post.id;
    var pub_date = post.pub_date;
    var title = post.title;
    var excerpt = post.excerpt;

    console.log(id);
    console.log(pub_date);
    console.log(title);
    console.log(excerpt);
});

The each loop doesn't seem to pull the through the JSON data?

0

2 Answers 2

2

You forgot one layer:

data = [
       ^  {
       |  ^  "attributes":{
       |  |  "posts":[
       |  |   ^
       1  2   3

giving you:

data[0].posts
 ^   ^    ^
 1   2    3
Sign up to request clarification or add additional context in comments.

Comments

0

You have to iterate through feeds[0]

var dfp_pos = feeds[0].attributes.dfp_pos;
var dfp_shortcode = feeds[0].attributes.dfp_shortcode;
var wildcard_pos = feeds[0].attributes.wildcard_pos;
var wildcard_shortcode = feeds[0].attributes.wildcard_shortcode;
var twitter_id = feeds[0].attributes.twitter_id;
var twitter_pos = feeds[0].attributes.twitter_pos;
var facebook_id = feeds[0].attributes.facebook_id;
var facebook_pos = feeds[0].attributes.facebook_pos;

jQuery.each(feeds[0].posts, function(i, post) {
    var id = post.id;
    var pub_date = post.pub_date;
    var title = post.title;
    var excerpt = post.excerpt;

    console.log(id);
    console.log(pub_date);
    console.log(title);
    console.log(excerpt);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.