将json数据附加到表中

时间:2013-02-05 07:22:55

标签: jquery html json

我是Jquery的新手,我的这个json文件看起来像这样。

 var json2 = {
        "data": [
                     {
                         "cid": "Movie",
                         "id": "/en/MIB",
                         "topic": "Men In Black",
                         "audience": [
                             {


                                 "userid": "100003914111287",
                                 "Name" : "Sandi Tan",
                                 "information": [
                                     {
                                         "category": "Athlete",
                                         "source": "Didier Drogba"
                                     },
                                     {
                                         "category": "Athlete",
                                         "source": "Frank Lampard"
                                     },
                                     {
                                         "category": "Professional sports team",
                                         "source": "Chelsea Football Club"
                                     },
                                     {
                                         "category": "favorite_teams",
                                         "source": "Chelsea Football Club"
                                     }
                                 ]
                             },
                             {
                                 "userid": "100003914111287",
                                 "Name": "Celia Tio",
                                 "information": [
                                     {
                                         "category": "Athlete",
                                         "source": "Didier Drogba"
                                     },
                                     {
                                         "category": "Athlete",
                                         "source": "Frank Lampard"
                                     },
                                     {
                                         "category": "Professional sports team",
                                         "source": "Chelsea Football Club"
                                     },
                                     {
                                         "category": "favorite_teams",
                                         "source": "Chelsea Football Club"
                                     }
                                 ]
                             }
                         ],
                         "type": "/soccer/football_team"
                     },
                     {


                         "id": "/en/Harry Potter",
                         "topic": "Harry Potter and the goblet of fire",
                         "audience": [
                             {
                                 "userid": "100003914111287",
                                 "Name": "Gibson Tay",

                                 "information": [
                                     {
                                         "category": "Athlete",
                                         "source": "Ryan Giggs"
                                     },
                                     {
                                         "category": "Professional sports team",
                                         "source": "Manchester United"
                                     },
                                     {
                                         "category": "favorite_teams",
                                         "source": "Manchester United"
                                     }
                                 ],
                                 "userid": "100003921730958"
                             },

                             {
                                 "userid": "100003914111287",
                                 "Name": "James Tan",
                                 "information": [
                                     {
                                         "category": "Athlete",
                                         "source": "Didier Drogba"
                                     },
                                     {
                                         "category": "Athlete",
                                         "source": "Frank Lampard"
                                     },
                                     {
                                         "category": "Professional sports team",
                                         "source": "Chelsea Football Club"
                                     },
                                     {
                                         "category": "favorite_teams",
                                         "source": "Chelsea Football Club"
                                     }
                                 ]
                             }


                         ],
                         "type": "/soccer/football_team"
                     },

                     {
                         "cid": "Movie",
                         "id": "/en/Iron Man",
                         "topic": "Iron Man",
                         "audience": [
                             {
                                 "userid": "100003914111287",
                                 "Name" : "Edmund Tan",
                                 "information": [
                                     {
                                         "category": "Athlete",
                                         "source": "Didier Drogba"
                                     },
                                     {
                                         "category": "Athlete",
                                         "source": "Frank Lampard"
                                     },
                                     {
                                         "category": "Professional sports team",
                                         "source": "Chelsea Football Club"
                                     },
                                     {
                                         "category": "favorite_teams",
                                         "source": "Chelsea Football Club"
                                     }
                                 ]
                             },



                 ],

                         "type": "/soccer/football_team"
                     }
                 ]

    };

如何提取信息。我只想要主题名称(例如黑衣人)和人名(例如“Sandi Tan”)并附加到如下表格中:

**Men in Black, Iron man  Harry Potter**
Sandi Tan     Edmund Tan   Gibson Tay
Celia Tio                  James tan

1 个答案:

答案 0 :(得分:2)

json2.data[0].topic //to get Men in black
json2.data[0].audience[0].Name // to get Sandi Tan

要获取所有名称和主题,您可以使用循环

进行此操作
for(key in json2.data){
console.log(json2.data[key].topic);
console.log(json2.data[key].audience[0].Name)
}

附加到表

$('<td> Audience : '+json2.data[0].audience[0].Name+'</td><td>Topic : '+json2.data[0].topic+'</td>').appendTo(tableid);

动态添加到表格

var td ="";
for(key in json2.data){
td += "<tr><td>"+json2.data[key].topic+"</td><td>"+json2.data[key].audience[0].Name+"</td></tr>";
}
$(td).appendTo('body');