2

I just finished tutorials for jquery ajax working with json. I had been able to create a ajax page. But to make things more complex. I have a json file that contain two types of array. example below(first objects belong to people array and the next two belong to newsfeed):

{  
   "people":[  
      {  
         "name":"John Constable",
         "occupation":"CEO",
         "location":"Toronto, Canada",
         "division":"Health & Epidemics",
         "profileImage":"",
         "followers":123
      },
      {  
         "name":"Edward Monet",
         "occupation":"Lead Developer",
         "location":"Toronto, Canada",
         "division":"Health & Epidemics",
         "profileImage":"",
         "followers":9923
      }
   ]
},
{  
   "newsfeed":[  
      {  
         "Title":"Evaluations of disaster education programs for children",
         "date":"Dec 10, 2015",
         "tags":[  
            "disaster",
            "tornado",
            "risk management"
         ],
         "division":"Health & Epidemics",
         "Image":"",
         "share":123
      },
      {  
         "Title":"UNISDR Annual Report 2014",
         "date":"Dec 10, 2015",
         "tags":[  
            "disaster",
            "tornado",
            "risk management"
         ],
         "division":"Civil & Cyber Security",
         "Image":"",
         "share":123
      }
   ]
}

My jquery code now is

$.getJSON( "ajax/result1-F.json", function( index ) {
    var jasondata = [];

    $.each(index, function( i, val ) {
           $container1.append( "<div><ul>" + val.profileImage + "</ul><ul><li>"+       val.name + "</li><li>" + val.occupation +"</li><li>"  + val.location + "</li><li>" + val.division + "</li></ul>" + "<ul><li> <button> FOLLOW </button> </li>" + "<li>" + val.followers + "</li><li>followers</li><ul></div>");
    }); 

});

The container1 display all the info for the people's array. Now I am stuck on how to display the newsfeed in a separate container2 simultaneously.

Can anyone point me to the correct direct? Thank you

2
  • Are you always going to get two arrays of people and newsfeed? Is index the entire JSON object? Commented Jan 20, 2016 at 22:48
  • actually I have a dropdown menu that influence both arrays, In this case I just want to figure out a simple way to display the two arrays in two different tables. and Yes Index is the entire JSON object. Commented Jan 20, 2016 at 22:56

3 Answers 3

1

I don't see why you couldn't just do something like this:

var jsonObj = {
    "people": [{
        "name": "John Constable",
        "occupation": "CEO",
        "location": "Toronto, Canada",
        "division": "Health & Epidemics",
        "profileImage": "",
        "followers": 123
    }, {
        "name": "Edward Monet",
        "occupation": "Lead Developer",
        "location": "Toronto, Canada",
        "division": "Health & Epidemics",
        "profileImage": "",
        "followers": 9923
    }]
},
{
    "newsfeed": [{
        "Title": "Evaluations of disaster education programs for children",
        "date": "Dec 10, 2015",
        "tags": ["disaster", "tornado", "risk management"],
        "division": "Health & Epidemics",
        "Image": "",
        "share": 123
    }, {
        "Title": "UNISDR Annual Report 2014",
        "date": "Dec 10, 2015",
        "tags": ["disaster", "tornado", "risk management"],
        "division": "Civil & Cyber Security",
        "Image": "",
        "share": 123
    }]
};

var people = jsonObj.people;
var newsfeed = jsonObj.newsfeed;

people.map(function(person) {
    $container1.append("<div><ul>" + person.profileImage + "</ul><ul><li>" + person.name + "</li><li>" + person.occupation + "</li><li>" + person.location + "</li><li>" + person.division + "</li></ul>" + "<ul><li> <button> FOLLOW </button> </li>" + "<li>" + person.followers + "</li><li>followers</li><ul></div>");
});

newsfeeds.map(function(news) {
    //do stuff
});
Sign up to request clarification or add additional context in comments.

Comments

0

You're currently trying to loop over the entire JSON object. Instead select that property from the object and loop over that.

$.each(index.people, function( i, val ) {//what you already have});

And then do something similar for the newsfeed

$.each(index.newsfeed, function( i, val ) {//printing what you have in newsfeed into $container2});

Comments

0

I don't understand. Why don't you join the two arrays?

{
    "people": [{...}, {...}],
    "newsfeed": [{...}, {...}]
}

Now, use jasondata.people[0].name. And for newsfeed: jasondata.newsfeed[0].Title.

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.