Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我想将JSON对象中的特定内容显示到表结构中?我该怎么做?_Javascript_Jquery_Html_Json - Fatal编程技术网

Javascript 我想将JSON对象中的特定内容显示到表结构中?我该怎么做?

Javascript 我想将JSON对象中的特定内容显示到表结构中?我该怎么做?,javascript,jquery,html,json,Javascript,Jquery,Html,Json,这是JSON对象。我想将所有的符号名、高价和低价存储在单独的变量中,并在我的页面中以表结构显示它们,以符号、高价和低价作为列?我该怎么做?我一次只能访问一个符号,但我希望显示所有符号?假设您的对象存在于如下数组中 {"symbol":"DRREDDY","series":"EQ","openPrice":"3,132.00","highPrice":"3,229.90","lowPrice":"3,132.00","ltp":"3,206.35","previousPrice":"3,153.2

这是JSON对象。我想将所有的符号名、高价和低价存储在单独的变量中,并在我的页面中以表结构显示它们,以符号、高价和低价作为列?我该怎么做?我一次只能访问一个符号,但我希望显示所有符号?

假设您的对象存在于如下数组中

{"symbol":"DRREDDY","series":"EQ","openPrice":"3,132.00","highPrice":"3,229.90","lowPrice":"3,132.00","ltp":"3,206.35","previousPrice":"3,153.25","netPrice":"1.68","tradedQuantity":"74,165","turnoverInLakhs":"2,379.33","lastCorpAnnouncementDate":"18-Jul-2016","lastCorpAnnouncement":"Annual General Meeting\/ Dividend - Rs 20\/- PerShare"},{"symbol":"ACC","series":"EQ","openPrice":"1,567.00","highPrice":"1,606.85","lowPrice":"1,564.85","ltp":"1,594.25","previousPrice":"1,568.10","netPrice":"1.67","tradedQuantity":"1,03,292","turnoverInLakhs":"1,645.62","lastCorpAnnouncementDate":"22-Feb-2016","lastCorpAnnouncement":"Dividend - Rs 6\/- Per Share"},
{"symbol":"ACC","series":"EQ","openPrice":"1,567.00","highPrice":"1,606.85","lowPrice":"1,564.85","ltp":"1,594.25","previousPrice":"1,568.10","netPrice":"1.67","tradedQuantity":"1,03,292","turnoverInLakhs":"1,645.62","lastCorpAnnouncementDate":"22-Feb-2016","lastCorpAnnouncement":"Dividend - Rs 6\/- Per Share"}
var arr = [{
    "symbol":"DRREDDY",
    "series":"EQ",
    "openPrice":"3,132.00",
    "highPrice":"3,229.90",
    "lowPrice":"3,132.00",
    "ltp":"3,206.35",
    "previousPrice":"3,153.25",
    "netPrice":"1.68",
    "tradedQuantity":"74,165",
    "turnoverInLakhs":"2,379.33",
    "lastCorpAnnouncementDate":"18-Jul-2016",
    "lastCorpAnnouncement":"Annual General Meeting\/ Dividend - Rs 20\/- PerShare"
},
{
    "symbol":"ACC",
    "series":"EQ",
    "openPrice":"1,567.00",
    "highPrice":"1,606.85",
    "lowPrice":"1,564.85",
    "ltp":"1,594.25",
    "previousPrice":"1,568.10",
    "netPrice":"1.67",
    "tradedQuantity":"1,03,292",
    "turnoverInLakhs":"1,645.62",
    "lastCorpAnnouncementDate":"22-Feb-2016",
    "lastCorpAnnouncement":"Dividend - Rs 6\/- Per Share"
},

{
    "symbol":"ACC",
    "series":"EQ",
    "openPrice":"1,567.00",
    "highPrice":"1,606.85",
    "lowPrice":"1,564.85",
    "ltp":"1,594.25",
    "previousPrice":"1,568.10",
    "netPrice":"1.67",
    "tradedQuantity":"1,03,292",
    "turnoverInLakhs":"1,645.62",
    "lastCorpAnnouncementDate":"22-Feb-2016",
    "lastCorpAnnouncement":"Dividend - Rs 6\/- Per Share"
}]
可以在数组中循环以绘制行 并通过对象循环绘制列,如下所示

{"symbol":"DRREDDY","series":"EQ","openPrice":"3,132.00","highPrice":"3,229.90","lowPrice":"3,132.00","ltp":"3,206.35","previousPrice":"3,153.25","netPrice":"1.68","tradedQuantity":"74,165","turnoverInLakhs":"2,379.33","lastCorpAnnouncementDate":"18-Jul-2016","lastCorpAnnouncement":"Annual General Meeting\/ Dividend - Rs 20\/- PerShare"},{"symbol":"ACC","series":"EQ","openPrice":"1,567.00","highPrice":"1,606.85","lowPrice":"1,564.85","ltp":"1,594.25","previousPrice":"1,568.10","netPrice":"1.67","tradedQuantity":"1,03,292","turnoverInLakhs":"1,645.62","lastCorpAnnouncementDate":"22-Feb-2016","lastCorpAnnouncement":"Dividend - Rs 6\/- Per Share"},
{"symbol":"ACC","series":"EQ","openPrice":"1,567.00","highPrice":"1,606.85","lowPrice":"1,564.85","ltp":"1,594.25","previousPrice":"1,568.10","netPrice":"1.67","tradedQuantity":"1,03,292","turnoverInLakhs":"1,645.62","lastCorpAnnouncementDate":"22-Feb-2016","lastCorpAnnouncement":"Dividend - Rs 6\/- Per Share"}
var arr = [{
    "symbol":"DRREDDY",
    "series":"EQ",
    "openPrice":"3,132.00",
    "highPrice":"3,229.90",
    "lowPrice":"3,132.00",
    "ltp":"3,206.35",
    "previousPrice":"3,153.25",
    "netPrice":"1.68",
    "tradedQuantity":"74,165",
    "turnoverInLakhs":"2,379.33",
    "lastCorpAnnouncementDate":"18-Jul-2016",
    "lastCorpAnnouncement":"Annual General Meeting\/ Dividend - Rs 20\/- PerShare"
},
{
    "symbol":"ACC",
    "series":"EQ",
    "openPrice":"1,567.00",
    "highPrice":"1,606.85",
    "lowPrice":"1,564.85",
    "ltp":"1,594.25",
    "previousPrice":"1,568.10",
    "netPrice":"1.67",
    "tradedQuantity":"1,03,292",
    "turnoverInLakhs":"1,645.62",
    "lastCorpAnnouncementDate":"22-Feb-2016",
    "lastCorpAnnouncement":"Dividend - Rs 6\/- Per Share"
},

{
    "symbol":"ACC",
    "series":"EQ",
    "openPrice":"1,567.00",
    "highPrice":"1,606.85",
    "lowPrice":"1,564.85",
    "ltp":"1,594.25",
    "previousPrice":"1,568.10",
    "netPrice":"1.67",
    "tradedQuantity":"1,03,292",
    "turnoverInLakhs":"1,645.62",
    "lastCorpAnnouncementDate":"22-Feb-2016",
    "lastCorpAnnouncement":"Dividend - Rs 6\/- Per Share"
}]

假设对象存在于如下所示的数组中

{"symbol":"DRREDDY","series":"EQ","openPrice":"3,132.00","highPrice":"3,229.90","lowPrice":"3,132.00","ltp":"3,206.35","previousPrice":"3,153.25","netPrice":"1.68","tradedQuantity":"74,165","turnoverInLakhs":"2,379.33","lastCorpAnnouncementDate":"18-Jul-2016","lastCorpAnnouncement":"Annual General Meeting\/ Dividend - Rs 20\/- PerShare"},{"symbol":"ACC","series":"EQ","openPrice":"1,567.00","highPrice":"1,606.85","lowPrice":"1,564.85","ltp":"1,594.25","previousPrice":"1,568.10","netPrice":"1.67","tradedQuantity":"1,03,292","turnoverInLakhs":"1,645.62","lastCorpAnnouncementDate":"22-Feb-2016","lastCorpAnnouncement":"Dividend - Rs 6\/- Per Share"},
{"symbol":"ACC","series":"EQ","openPrice":"1,567.00","highPrice":"1,606.85","lowPrice":"1,564.85","ltp":"1,594.25","previousPrice":"1,568.10","netPrice":"1.67","tradedQuantity":"1,03,292","turnoverInLakhs":"1,645.62","lastCorpAnnouncementDate":"22-Feb-2016","lastCorpAnnouncement":"Dividend - Rs 6\/- Per Share"}
var arr = [{
    "symbol":"DRREDDY",
    "series":"EQ",
    "openPrice":"3,132.00",
    "highPrice":"3,229.90",
    "lowPrice":"3,132.00",
    "ltp":"3,206.35",
    "previousPrice":"3,153.25",
    "netPrice":"1.68",
    "tradedQuantity":"74,165",
    "turnoverInLakhs":"2,379.33",
    "lastCorpAnnouncementDate":"18-Jul-2016",
    "lastCorpAnnouncement":"Annual General Meeting\/ Dividend - Rs 20\/- PerShare"
},
{
    "symbol":"ACC",
    "series":"EQ",
    "openPrice":"1,567.00",
    "highPrice":"1,606.85",
    "lowPrice":"1,564.85",
    "ltp":"1,594.25",
    "previousPrice":"1,568.10",
    "netPrice":"1.67",
    "tradedQuantity":"1,03,292",
    "turnoverInLakhs":"1,645.62",
    "lastCorpAnnouncementDate":"22-Feb-2016",
    "lastCorpAnnouncement":"Dividend - Rs 6\/- Per Share"
},

{
    "symbol":"ACC",
    "series":"EQ",
    "openPrice":"1,567.00",
    "highPrice":"1,606.85",
    "lowPrice":"1,564.85",
    "ltp":"1,594.25",
    "previousPrice":"1,568.10",
    "netPrice":"1.67",
    "tradedQuantity":"1,03,292",
    "turnoverInLakhs":"1,645.62",
    "lastCorpAnnouncementDate":"22-Feb-2016",
    "lastCorpAnnouncement":"Dividend - Rs 6\/- Per Share"
}]
可以在数组中循环以绘制行 并通过对象循环绘制列,如下所示

{"symbol":"DRREDDY","series":"EQ","openPrice":"3,132.00","highPrice":"3,229.90","lowPrice":"3,132.00","ltp":"3,206.35","previousPrice":"3,153.25","netPrice":"1.68","tradedQuantity":"74,165","turnoverInLakhs":"2,379.33","lastCorpAnnouncementDate":"18-Jul-2016","lastCorpAnnouncement":"Annual General Meeting\/ Dividend - Rs 20\/- PerShare"},{"symbol":"ACC","series":"EQ","openPrice":"1,567.00","highPrice":"1,606.85","lowPrice":"1,564.85","ltp":"1,594.25","previousPrice":"1,568.10","netPrice":"1.67","tradedQuantity":"1,03,292","turnoverInLakhs":"1,645.62","lastCorpAnnouncementDate":"22-Feb-2016","lastCorpAnnouncement":"Dividend - Rs 6\/- Per Share"},
{"symbol":"ACC","series":"EQ","openPrice":"1,567.00","highPrice":"1,606.85","lowPrice":"1,564.85","ltp":"1,594.25","previousPrice":"1,568.10","netPrice":"1.67","tradedQuantity":"1,03,292","turnoverInLakhs":"1,645.62","lastCorpAnnouncementDate":"22-Feb-2016","lastCorpAnnouncement":"Dividend - Rs 6\/- Per Share"}
var arr = [{
    "symbol":"DRREDDY",
    "series":"EQ",
    "openPrice":"3,132.00",
    "highPrice":"3,229.90",
    "lowPrice":"3,132.00",
    "ltp":"3,206.35",
    "previousPrice":"3,153.25",
    "netPrice":"1.68",
    "tradedQuantity":"74,165",
    "turnoverInLakhs":"2,379.33",
    "lastCorpAnnouncementDate":"18-Jul-2016",
    "lastCorpAnnouncement":"Annual General Meeting\/ Dividend - Rs 20\/- PerShare"
},
{
    "symbol":"ACC",
    "series":"EQ",
    "openPrice":"1,567.00",
    "highPrice":"1,606.85",
    "lowPrice":"1,564.85",
    "ltp":"1,594.25",
    "previousPrice":"1,568.10",
    "netPrice":"1.67",
    "tradedQuantity":"1,03,292",
    "turnoverInLakhs":"1,645.62",
    "lastCorpAnnouncementDate":"22-Feb-2016",
    "lastCorpAnnouncement":"Dividend - Rs 6\/- Per Share"
},

{
    "symbol":"ACC",
    "series":"EQ",
    "openPrice":"1,567.00",
    "highPrice":"1,606.85",
    "lowPrice":"1,564.85",
    "ltp":"1,594.25",
    "previousPrice":"1,568.10",
    "netPrice":"1.67",
    "tradedQuantity":"1,03,292",
    "turnoverInLakhs":"1,645.62",
    "lastCorpAnnouncementDate":"22-Feb-2016",
    "lastCorpAnnouncement":"Dividend - Rs 6\/- Per Share"
}]
将列表迭代到表

var tableData = ''
arr.map((x) => {
tableData += '<tr><td>' + x.highPrice + '</td><td>' + x.lowPrice + '</td></tr>';  
});
$('table').find('tbody').html(tableData)
将列表迭代到表

var tableData = ''
arr.map((x) => {
tableData += '<tr><td>' + x.highPrice + '</td><td>' + x.lowPrice + '</td></tr>';  
});
$('table').find('tbody').html(tableData)

您可能不需要为symbol、highPrice和lowPrice创建单独的数组

相反,您可以使用循环遍历json数组并获取每个项

var table = document.getElementById("myTable");
for(var i =0; i < data.length; i++) {
    var row = table.insertRow(0);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(2);
    cell1.innerHTML =  data[i].symbol;
    cell2.innerHTML = data[i].highPrice;
    cell3.innerHTML = data[i].lowPrice;
}

您可能不需要为symbol、highPrice和lowPrice创建单独的数组

相反,您可以使用循环遍历json数组并获取每个项

var table = document.getElementById("myTable");
for(var i =0; i < data.length; i++) {
    var row = table.insertRow(0);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(2);
    cell1.innerHTML =  data[i].symbol;
    cell2.innerHTML = data[i].highPrice;
    cell3.innerHTML = data[i].lowPrice;
}
试试这个:

var _tbody = $('tbody');
_myJson.forEach(function(item){
var _tr = '<tr><td>'+item.symbol+'</td><td>'+item.highPrice+'</td><td>'+item.lowPrice+'</td></tr>'
_tbody+=_tr
})
$('#demoTable').append(_tbody)
试试这个:

var _tbody = $('tbody');
_myJson.forEach(function(item){
var _tr = '<tr><td>'+item.symbol+'</td><td>'+item.highPrice+'</td><td>'+item.lowPrice+'</td></tr>'
_tbody+=_tr
})
$('#demoTable').append(_tbody)
考虑

var data = [{
    "symbol":"DRREDDY",
    "series":"EQ",
    "openPrice":"3,132.00",
    "highPrice":"3,229.90",
    "lowPrice":"3,132.00",
    "ltp":"3,206.35",
    "previousPrice":"3,153.25",
    "netPrice":"1.68",
    "tradedQuantity":"74,165",
    "turnoverInLakhs":"2,379.33",
    "lastCorpAnnouncementDate":"18-Jul-2016",
    "lastCorpAnnouncement":"Annual General Meeting\/ Dividend - Rs 20\/- PerShare"
    },
    ...
];

var table = $('<table>');
table.append('<thead><tr><td>symbol</td><td>highPrice</td><td>lowPrice</td></tr></thead>');
for(var i in data){
    var tr = $('<tr>');
    var td1 = '<td>' + data[i].symbol;
    var td2 = '<td>' + data[i].highPrice;
    var td3 = '<td>' + data[i].lowPrice;
    tr.append(td1,td2,td3);
    table.append(tr);
}

$('#your-panel').append(table);
要在表u中显示,可以在for循环中使用如下显示功能

var result= {"symbol":"DRREDDY","series":"EQ","openPrice":"3,132.00","highPrice":"3,229.90","lowPrice":"3,132.00","ltp":"3,206.35","previousPrice":"3,153.25","netPrice":"1.68","tradedQuantity":"74,165","turnoverInLakhs":"2,379.33","lastCorpAnnouncementDate":"18-Jul-2016","lastCorpAnnouncement":"Annual General Meeting\/ Dividend - Rs 20\/- PerShare"},

{"symbol":"ACC","series":"EQ","openPrice":"1,567.00","highPrice":"1,606.85","lowPrice":"1,564.85","ltp":"1,594.25","previousPrice":"1,568.10","netPrice":"1.67","tradedQuantity":"1,03,292","turnoverInLakhs":"1,645.62","lastCorpAnnouncementDate":"22-Feb-2016","lastCorpAnnouncement":"Dividend - Rs 6\/- Per Share"},
{"symbol":"ACC","series":"EQ","openPrice":"1,567.00","highPrice":"1,606.85","lowPrice":"1,564.85","ltp":"1,594.25","previousPrice":"1,568.10","netPrice":"1.67","tradedQuantity":"1,03,292","turnoverInLakhs":"1,645.62","lastCorpAnnouncementDate":"22-Feb-2016","lastCorpAnnouncement":"Dividend - Rs 6\/- Per Share"}

var low_price,high_price,symbols=[];

for(var i=0; i<result.length; i++){
low_price[i]=result[i].lowPrice;
}
在html页面中

for(var i=0; i<result.length; i++){
//low_price[i]=result[i].lowPrice;
display(result[i]);
}

function display(result){
var rows='<tr>'+
'<td>'+result.symbol+'</td>'+
'<td>'+result.highPrice+'</td>'+
'<td>'+result.lowPrice+'</td>'+
'</tr>';
$('#result').append(rows);

}
我想你在找这个。。否则,请为错误的信息道歉

请提供您想要考虑的更多信息

var data = [{
    "symbol":"DRREDDY",
    "series":"EQ",
    "openPrice":"3,132.00",
    "highPrice":"3,229.90",
    "lowPrice":"3,132.00",
    "ltp":"3,206.35",
    "previousPrice":"3,153.25",
    "netPrice":"1.68",
    "tradedQuantity":"74,165",
    "turnoverInLakhs":"2,379.33",
    "lastCorpAnnouncementDate":"18-Jul-2016",
    "lastCorpAnnouncement":"Annual General Meeting\/ Dividend - Rs 20\/- PerShare"
    },
    ...
];

var table = $('<table>');
table.append('<thead><tr><td>symbol</td><td>highPrice</td><td>lowPrice</td></tr></thead>');
for(var i in data){
    var tr = $('<tr>');
    var td1 = '<td>' + data[i].symbol;
    var td2 = '<td>' + data[i].highPrice;
    var td3 = '<td>' + data[i].lowPrice;
    tr.append(td1,td2,td3);
    table.append(tr);
}

$('#your-panel').append(table);
要在表u中显示,可以在for循环中使用如下显示功能

var result= {"symbol":"DRREDDY","series":"EQ","openPrice":"3,132.00","highPrice":"3,229.90","lowPrice":"3,132.00","ltp":"3,206.35","previousPrice":"3,153.25","netPrice":"1.68","tradedQuantity":"74,165","turnoverInLakhs":"2,379.33","lastCorpAnnouncementDate":"18-Jul-2016","lastCorpAnnouncement":"Annual General Meeting\/ Dividend - Rs 20\/- PerShare"},

{"symbol":"ACC","series":"EQ","openPrice":"1,567.00","highPrice":"1,606.85","lowPrice":"1,564.85","ltp":"1,594.25","previousPrice":"1,568.10","netPrice":"1.67","tradedQuantity":"1,03,292","turnoverInLakhs":"1,645.62","lastCorpAnnouncementDate":"22-Feb-2016","lastCorpAnnouncement":"Dividend - Rs 6\/- Per Share"},
{"symbol":"ACC","series":"EQ","openPrice":"1,567.00","highPrice":"1,606.85","lowPrice":"1,564.85","ltp":"1,594.25","previousPrice":"1,568.10","netPrice":"1.67","tradedQuantity":"1,03,292","turnoverInLakhs":"1,645.62","lastCorpAnnouncementDate":"22-Feb-2016","lastCorpAnnouncement":"Dividend - Rs 6\/- Per Share"}

var low_price,high_price,symbols=[];

for(var i=0; i<result.length; i++){
low_price[i]=result[i].lowPrice;
}
在html页面中

for(var i=0; i<result.length; i++){
//low_price[i]=result[i].lowPrice;
display(result[i]);
}

function display(result){
var rows='<tr>'+
'<td>'+result.symbol+'</td>'+
'<td>'+result.highPrice+'</td>'+
'<td>'+result.lowPrice+'</td>'+
'</tr>';
$('#result').append(rows);

}
我想你在找这个。。否则,请为错误的信息道歉



请提供您想要的、不是JSON对象的更多信息。只包含几个方法,在页面上没有什么有趣的显示。它将是JSON对象的列表,对吗?我的任务是分别打印文本文件中给出的符号名称、高价格和低价格。我将文本文件解析为JSON对象,所以现在我想显示它,它不是JSON对象。只包含几个方法,在页面上没有什么有趣的显示。它将是JSON对象列表,对吗?我的任务是打印符号名称,在文本文件中分别给出的高和低价格我将文本文件解析为json对象,所以现在我想显示它您的面板是什么??div或您想将表添加到其中的任何部分,我通过'id=您的面板来提及它@Karthikeyan_kkall工作完美我在一个按钮上添加了这段代码,每当我点击这个函数时,目录会反复打印如何停止,我更新我的小提琴,你可以看到@Karthikeyankku感谢更新我想问的是,每当我按下add table(添加表格)按钮时,表格都会继续正确添加。在我的例子中,我有两个按钮,两个按钮都显示不同的表格,因此,在按下按钮一次后,如果我再次按下该按钮,它将不会再次打印表格。对此,我应该做些什么?您的面板是什么?div或您希望将表格添加到其中的任何部分,我通过'id=您的面板来提及它@Karthikeyan_kkall工作完美我在一个按钮上添加了这段代码,每当我点击这个函数时,目录会反复打印如何停止,我更新我的小提琴,你可以看到@Karthikeyankku感谢更新我想问的是,每当我按下add table(添加表格)按钮时,表格都会继续正确添加。在我的情况下,我有两个按钮,都显示不同的表格,因此,在按下按钮一次后,如果我再次按下该按钮,它将不会再次打印表格。对此,我该怎么办?请重新检查我的答案谢谢你的答案请重新检查我的答案谢谢你的答案你的答案工作正常我将所有这些功能保存在按钮称为显示表。如果我多次按下该按钮,目录就会重复,如何停止?您的答案工作正常,我将所有这些功能都保存在一个名为“显示表”的按钮中。如果我多次按下该按钮,目录将重复,如何停止该操作??