2016-01-20 56 views
2

我剛剛完成了jquery ajax與json一起工作的教程。我已經能夠創建一個Ajax頁面。但要讓事情更復雜。我有一個包含兩種數組的json文件。下面的例子(第一對象屬於人民陣列和接下來的兩個屬於新聞源):Jquery ajax使用不同的命名數組解析json文件

{ 
    "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 
     } 
    ] 
} 

我的jQuery代碼現在

$.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>"); 
    }); 

}); 

container1顯示了人們的數組中的所有信息的。 現在我陷入瞭如何同時在單獨的container2中顯示新聞源。

任何人都可以指向正確的直接?謝謝

+0

你總是會得到兩個'people'和'newsfeed'的數組嗎? 'index'是整個JSON對象嗎? –

+0

實際上我有一個影響兩個數組的下拉菜單,在這種情況下,我只想弄清楚一個簡單的方法來在兩個不同的表中顯示兩個數組。和是索引是整個JSON對象。 – tiancaishi

回答

1

我不明白爲什麼你不能只是做這樣的事情:

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 
}); 
0

您目前正試圖遍歷整個JSON對象。相反,從對象中選擇該屬性並循環。

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

然後做新聞源

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

我不明白,類似的東西。你爲什麼不加入這兩個陣列?

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

現在,使用jasondata.people[0].name。對於新聞源:jasondata.newsfeed[0].Title