【发布时间】:2021-06-30 18:04:03
【问题描述】:
我正在尝试将 json 数据显示到 html 表中。这是我的代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.getJSON("https://blockchain.info/unconfirmed-transactions?format=json", function(result){
$.each(result, function(i, field){
$("div").append(field + " ");
});
});
});
});
</script>
但我得到了所有 [object, Object],然后我尝试制作这样的东西:
<table
id="table"
data-show-refresh="true"
data-auto-refresh="true"
data-pagination="true"
data-url="https://blockchain.info/unconfirmed-transactions?format=json"
data-side-pagination="server">
<thead>
<tr>
<th data-field="id" data-sortable="true">id</th>
<th data-field="addr" data-sortable="true">address</th>
<th data-field="value" data-sortable="true">price</th>
</tr>
</thead>
</table>
但最后一个甚至不显示任何内容。 我做错了什么?
我得到的唯一错误是 $ 不是函数:
$(function() {
$('#table').bootstrapTable()
})
这是json的示例:
{
"txs":[
{
"lock_time":0,
"ver":1,
"size":192,
"inputs":[
{
"sequence":4294967295,
"witness":"",
"prev_out":{
"spent":true,
"spending_outpoints":[
{
"tx_index":0,
"n":0
}
],
"tx_index":0,
"type":0,
"addr":"1JR1JWDones3w2xHBLgXPYtyyBfLGcNWZY",
"value":1205152,
"n":0,
"script":"76a914bf045f927639460dfca81c597c38d708ffa0173388ac"
},
"script":"483045022100f4385c5e87ab6bb780fbee525826e3abc7acd9f1a64f5ec89656f75fff65438502207c453930133eec554167ed4681f4310b628e80652f2d21b0b5d6577b07462d040121037aac503a15d029925f6300b7983010e1850f00d27e75ec37dddb76b65e1e0ff8"
}
],
"weight":768,
"time":1617660671,
"tx_index":0,
"vin_sz":1,
"hash":"3941a19e458794e933623af2b6b234af9f2192f4e19bd0782147fa792461a5d3",
"vout_sz":1,
"relayed_by":"0.0.0.0",
"out":[
{
"spent":false,
"tx_index":0,
"type":0,
"addr":"13BAFsVnZ3WeEGYEG7QFAWRyHLxLonbtcU",
"value":1192607,
"n":0,
"script":"76a91417dc2355fc4309873b191274986bfaac62ffd01c88ac"
}
]
},
}
我需要找到一种方法来获取所有地址的值。这是我最后一次尝试,但还是不行:
fetch('website')
.then(res => res.json())
.then((res) => {
const txs = res.txs;
getElement('div').innerHTML = 'Addr: ' + JSON.stringify(txs.prev_out.addr);
它告诉我不可能读取 addr 的属性。为什么?
【问题讨论】:
-
第一个行为意味着
field不是字符串。我们不知道它是什么,所以我建议你像$("div").append(JSON.stringify(field) + " ");那样输出它,然后至少你会看到field的对象结构,并可以决定如何深入到你真正想要显示的内容。 -
谢谢,知道它确实有效,我得到了所有的 json。怎么过滤才知道?例如,如果我只需要获取地址,我该怎么做?
-
您需要展示 JSON 样本,我们才能可靠地回答这个问题
-
这取决于你得到的结构。如果
address是field的属性,并且它是一个字符串值,那么field.address... 等等。但是查看一些关于从该站点上的对象中提取数据的问答... 这是非常基本的东西. -
我尝试了,但我没有定义。我从上面代码中的链接获取 json