简体   繁体   English

我想将JSON对象中的特定内容显示为表结构吗? 我怎样才能做到这一点?

[英]I want to display specific contents from JSON object into a table structure? how can i do that?

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

This is the JSON object.I want to store all the symbol names, high price and low price in separate variables and display those alone in my page in table structure with symbol, high price and low price as columns? 这是JSON对象,我想将所有符号名称,高价位和低价位存储在单独的变量中,并在我的页面中以符号,高价位和低价位作为表格结构单独显示在页面中? How can i do that ? 我怎样才能做到这一点 ? Am able to acces one symbol at a time but i want all the symbols to b displayed ? 一次可以添加一个符号,但是我希望所有符号都显示b吗?

Try this: 尝试这个:

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

There is Jsfiddle 有Jsfiddle

assuming your objects exist in an array like below 假设您的对象存在于如下数组中

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

you can loop through array to draw rows and loop through objects to draw columns like below 您可以遍历数组以绘制行并遍历对象以绘制列,如下所示

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

Iterate list to table 迭代列表到表

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

fiddle 小提琴

You may not need to create separate array for symbol, highPrice & lowPrice. 您可能不需要为符号,highPrice和lowPrice创建单独的数组。

Instead you can use forEach to loop through the json array and get each of the item 相反,您可以使用forEach遍历json数组并获取每个项目

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)

JSFIDDLE JSFIDDLE

consider 考虑

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

to display in table u can use a display function inside the for loop like this .. 要在表中显示u可以在for循环内使用显示功能,例如:

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

}

In html page 在html页面

<div>
<table>
<thead>
<th>Symbols</th>
<th>High price</th>
<th>Low price</th>
</thead>
<tbody id="result">
// your display function will be add rows using append function ...
</tbody>
</table>
</div

i guess ur looking for this .. otherwise sorry for the wrong information 我猜你在找这个..否则对不起错误的信息

please provide more information that u want 请提供您想要的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM