如何在html表中显示json数据?

时间:2016-03-15 07:30:27

标签: javascript php jquery html json

{
  "cache": true,
  "data": [
    {
      "unique_id": "958717",
      "description": "Central Zone (Bangladesh) 30 * v North Zone (Bangladesh) 378/10 ",
      "title": "Central Zone (Bangladesh) 30 * v North Zone (Bangladesh) 378/10 "
    },
    {
      "unique_id": "958715",
      "description": "South Zone (Bangladesh) v East Zone (Bangladesh) 373/8 *",
      "title": "South Zone (Bangladesh) v East Zone (Bangladesh) 373/8 *"
    },
    {
      "unique_id": "895971",
      "description": "Victoria v New South Wales 218/6 *",
      "title": "Victoria v New South Wales 218/6 *"
    },
    {
      "unique_id": "895969",
      "description": "South Australia 206/4 * v Tasmania 91/10 ",
      "title": "South Australia 206/4 * v Tasmania 91/10 "
    },
    {
      "unique_id": "895967",
      "description": "Queensland 147/10  v Western Australia 32/2 *",
      "title": "Queensland 147/10  v Western Australia 32/2 *"
    },
    {
      "unique_id": "971711",
      "description": "Mumbai Cricket Association XI v South Africa",
      "title": "Mumbai Cricket Association XI v South Africa"
    },
    {
      "unique_id": "951375",
      "description": "India Women v Bangladesh Women",
      "title": "India Women v Bangladesh Women"
    },
    {
      "unique_id": "951329",
      "description": "India v New Zealand",
      "title": "India v New Zealand"
    },
    {
      "unique_id": "951377",
      "description": "New Zealand Women v Sri Lanka Women",
      "title": "New Zealand Women v Sri Lanka Women"
    }
  ],
  "provider": {
    "pubDate": "2016-03-15T06:46:03.424Z",
    "source": "http://www.cricinfo.com/",
    "url": "http://crm.wherrelz.com/"
  }
}

我有这个json数据。我想在HTML表格中显示每个匹配的标题和描述。我使用了php的json_decode方法,它为我提供了这个:

array(3){[" cache"] => bool(true)[" data"] => array(9){[0] => array(3){[" unique_id"] => string(6)" 958717" ["描述"] => string(64)" Central Zone(Bangladesh)30 * v North Zone(Bangladesh)378/10" ["标题"] => string(64)" Central Zone(Bangladesh)30 * v North Zone(Bangladesh)378/10" } [1] => array(3){[" unique_id"] => string(6)" 958715" ["描述"] => string(56)" South Zone(Bangladesh)v East Zone(Bangladesh)373/8 *" ["标题"] => string(56)" South Zone(Bangladesh)v East Zone(Bangladesh)373/8 *" } [2] => array(3){[" unique_id"] => string(6)" 895971" ["描述"] => string(34)" Victoria v New South Wales 218/6 *" ["标题"] => string(34)" Victoria v New South Wales 218/6 *" } [3] => array(3){[" unique_id"] => string(6)" 895969" ["描述"] => string(41)" South Australia 206/4 * v Tasmania 91/10" ["标题"] => string(41)" South Australia 206/4 * v Tasmania 91/10" } [4] => array(3){[" unique_id"] => string(6)" 895967" ["描述"] => string(45)" Queensland 147/10 v Western Australia 32/2 *" ["标题"] => string(45)" Queensland 147/10 v Western Australia 32/2 *" } [5] => array(3){[" unique_id"] => string(6)" 971711" ["描述"] => string(44)"孟买板球协会XI诉南非" ["标题"] => string(44)"孟买板球协会XI诉南非" } [6] => array(3){[" unique_id"] => string(6)" 951375" ["描述"] => string(30)" India Women v Bangladesh Women" ["标题"] => string(30)" India Women v Bangladesh Women" } [7] => array(3){[" unique_id"] => string(6)" 951329" ["描述"] => string(19)" India v New Zealand" ["标题"] => string(19)" India v New Zealand" } [8] => array(3){[" unique_id"] => string(6)" 951377" ["描述"] => string(35)" New Zealand Women v Sri Lanka Women" ["标题"] => string(35)" New Zealand Women v Sri Lanka Women" }"提供商"] => array(3){[" pubDate"] => string(24)" 2016-03-15T06:46:03.424Z" ["源"] => string(24)" http://www.cricinfo.com/" [" URL"] => string(24)" http://crm.wherrelz.com/" }}

但现在如何在html表中获得每个板球的标题和描述。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

请检查以下代码

HTML

<div id="div1"> </div>

Jquery的

var datarecord=[
{
  "unique_id": "958717",
  "description": "Central Zone (Bangladesh) 30 * v North Zone (Bangladesh) 378/10 ",
  "title": "Central Zone (Bangladesh) 30 * v North Zone (Bangladesh) 378/10 "
},
{
  "unique_id": "958715",
  "description": "South Zone (Bangladesh) v East Zone (Bangladesh) 373/8 *",
  "title": "South Zone (Bangladesh) v East Zone (Bangladesh) 373/8 *"
},
{
  "unique_id": "895971",
  "description": "Victoria v New South Wales 218/6 *",
  "title": "Victoria v New South Wales 218/6 *"
},
{
  "unique_id": "895969",
  "description": "South Australia 206/4 * v Tasmania 91/10 ",
  "title": "South Australia 206/4 * v Tasmania 91/10 "
},
{
  "unique_id": "895967",
  "description": "Queensland 147/10  v Western Australia 32/2 *",
  "title": "Queensland 147/10  v Western Australia 32/2 *"
},
{
  "unique_id": "971711",
  "description": "Mumbai Cricket Association XI v South Africa",
  "title": "Mumbai Cricket Association XI v South Africa"
},
{
  "unique_id": "951375",
  "description": "India Women v Bangladesh Women",
  "title": "India Women v Bangladesh Women"
},
{
  "unique_id": "951329",
  "description": "India v New Zealand",
  "title": "India v New Zealand"
},
{
  "unique_id": "951377",
  "description": "New Zealand Women v Sri Lanka Women",
  "title": "New Zealand Women v Sri Lanka Women"
}
]
var records=$("<table/>").attr("id","tabcontent");
$("#record").append(records);
for(var j=0;j<datarecord.length;j++)
{
  var tr="<tr>";
  var td1="<td>"+datarecord[j]["unique_id"]+"</td>";
  var td2="<td>"+datarecord[j]["description"]+"</td>";
  var td3="<td>"+datarecord[j]["title"]+"</td></tr>";

  $("#tabcontent").append(tr+td1+td2+td3); 

}