首页 > 解决方案 > Json返回[对象,对象]

问题描述

我正在尝试将 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 的属性。为什么?

标签: phphtmljson

解决方案


再次检查https://blockchain.info/unconfirmed-transactions?format=json的响应,它将返回一个具有名为“txs”的属性的对象。在那里,您将拥有所有行,因此您必须在 txs 上循环,而不是直接在响应上


推荐阅读