简体   繁体   English

如何从 javascript 中的 json 中提取 json 值

[英]How to extract json values from json in javascript

Hello guys I am currently trying to phrase a json but it's hard for me.大家好,我目前正在尝试表达 json 但这对我来说很难。 The json response is json 响应为

{
"txs": 
{
    "lock_time": 0,
    "ver": 1,
    "size": 372,
    "inputs": [
        {
            "sequence": 4294967295,
            "prev_out": {
                "spent": true,
                "tx_index": 78636642,
                "type": 0,
                "addr": "1Dihat9Fy1ZDzFCq33LN5M7kzG3Fmi3FbZ",
                "value": 61140,
                "n": 1,
                "script": "76a9148b84711990f82d3cd70013e738787506a2156ebf88ac"
            },
            "script": "47304402203409c3381b75deac615630125c62af73e959e4e42431397209d0298da272c4b4022011720c0e8ecc8a4d01e1f6210891fe5e65f581473c05f0b15bc38010ca5155610121038b1c61898ba817c0361fb910c001cddf309388f6e156f96de749fdbb1c531f34"
        },
        {
            "sequence": 4294967295,
            "prev_out": {
                "spent": true,
                "tx_index": 78634898,
                "type": 0,
                "addr": "1Dihat9Fy1ZDzFCq33LN5M7kzG3Fmi3FbZ",
                "value": 379950,
                "n": 1,
                "script": "76a9148b84711990f82d3cd70013e738787506a2156ebf88ac"
            },
            "script": "473044022051029de181886a8225e78ea8c97fcdff4fdf65bd5479cf4370a8bf38ffd8770002202e788bb00aa4b017249eeefdfcf49cc2e591a7dbb25a1b2a3df924505b7683a50121038b1c61898ba817c0361fb910c001cddf309388f6e156f96de749fdbb1c531f34"
        }
    ,
    "doublespend": false,
    "time": 1424718521,
    "txindex": 78637260,
    "vinsz": 2,
    "hash": "011931da4d5ef3afde1b043f285b27cec2883b9d77feda71fe67b13341778494",
    "voutsz": 2,
    "relayedby": "127.0.0.1",
    "out": [
        {
            "addrtaglink": "http://luckyb.it/",
            "addrtag": "LuckyBit blue",
            "spent": false,
            "txindex": 78637260,
            "type": 0,
            "addr": "1LuckyB5VGzdZLZSBZvw8DR17iiFCpST7L",
            "value": 356450,
            "n": 0,
            "script": "76a914da5dde86d69a5d9dad88763f2df4b048953c7d0488ac"
        },
        {
            "spent": false,
            "txindex": 78637260,
            "type": 0,
            "addr": "1Dihat9Fy1ZDzFCq33LN5M7kzG3Fmi3FbZ",
            "value": 74640,
            "n": 1,
            "script": "76a9148b84711990f82d3cd70013e738787506a2156ebf88ac"
        }
    ]
}
]
}

This json is stored in a variable content .这个 json 存储在变量content中。 By let content = JSON.parse(result)通过让内容 = JSON.parse(result)

I used var data = content.txs.inputs.addr我用var data = content.txs.inputs.addr

to take out the info from that but it shows nothing, null.从中取出信息,但它什么也没显示,null。 I also tried only content and it works, but it doesn't work with content.txs.inputs.addr .我也只尝试了content并且它有效,但它不适用于content.txs.inputs.addr Can any JavaScript developers help me solve this?任何 JavaScript 开发人员可以帮我解决这个问题吗? I'm facing this type of json for the first time.我第一次面对这种类型的 json。

content.txs.inputs is an array. content.txs.inputs是一个数组。 Did you mean var data = content.txs.inputs[0].prev_out.addr;你的意思是var data = content.txs.inputs[0].prev_out.addr; ? ?

You need to go down a couple more steps你需要 go 再走几步

inputs is an array, so you need a subscript, then two more levels of keys: inputs是一个数组,所以你需要一个下标,然后是两级键:

Var data = content.txs.inputs[0].prev_out.addr

Note that there is an addr value in both the inputs elements.请注意,两个inputs元素中都有一个addr值。 Change the subscript to get the other one.更改下标以获取另一个下标。

element inputs is an arrray, so you need access each postion in array, you can use for loop for element inputs, access each position such as inputs[0].addr, inputs[1].addr元素输入是一个数组,因此您需要访问数组中的每个位置,您可以使用for循环进行元素输入,访问每个position,例如输入[0].addr,输入[1].addr

  1. The object model for the inputs array don't have any addr property, you need to access the prev_out object property to get the addr value.输入数组的 object model 没有任何 addr 属性,您需要访问 prev_out object 属性来获取 addr 值。

  2. You are missing an closing ] in the inputs array, and you need to remove the ] at the end of the json file.您在输入数组中缺少关闭 ],您需要删除 json 文件末尾的 ]。

After that you can get the addr property in the next way:之后,您可以通过以下方式获取 addr 属性:

 const content = { "txs": { "lock_time": 0, "ver": 1, "size": 372, "inputs": [ { "sequence": 4294967295, "prev_out": { "spent": true, "tx_index": 78636642, "type": 0, "addr": "1Dihat9Fy1ZDzFCq33LN5M7kzG3Fmi3FbZ", "value": 61140, "n": 1, "script": "76a9148b84711990f82d3cd70013e738787506a2156ebf88ac" }, "script": "47304402203409c3381b75deac615630125c62af73e959e4e42431397209d0298da272c4b4022011720c0e8ecc8a4d01e1f6210891fe5e65f581473c05f0b15bc38010ca5155610121038b1c61898ba817c0361fb910c001cddf309388f6e156f96de749fdbb1c531f34" }, { "sequence": 4294967295, "prev_out": { "spent": true, "tx_index": 78634898, "type": 0, "addr": "1Dihat9Fy1ZDzFCq33LN5M7kzG3Fmi3FbZ", "value": 379950, "n": 1, "script": "76a9148b84711990f82d3cd70013e738787506a2156ebf88ac" }, "script": "473044022051029de181886a8225e78ea8c97fcdff4fdf65bd5479cf4370a8bf38ffd8770002202e788bb00aa4b017249eeefdfcf49cc2e591a7dbb25a1b2a3df924505b7683a50121038b1c61898ba817c0361fb910c001cddf309388f6e156f96de749fdbb1c531f34" }], "doublespend": false, "time": 1424718521, "txindex": 78637260, "vinsz": 2, "hash": "011931da4d5ef3afde1b043f285b27cec2883b9d77feda71fe67b13341778494", "voutsz": 2, "relayedby": "127.0.0.1", "out": [ { "addrtaglink": "http://luckyb.it/", "addrtag": "LuckyBit blue", "spent": false, "txindex": 78637260, "type": 0, "addr": "1LuckyB5VGzdZLZSBZvw8DR17iiFCpST7L", "value": 356450, "n": 0, "script": "76a914da5dde86d69a5d9dad88763f2df4b048953c7d0488ac" }, { "spent": false, "txindex": 78637260, "type": 0, "addr": "1Dihat9Fy1ZDzFCq33LN5M7kzG3Fmi3FbZ", "value": 74640, "n": 1, "script": "76a9148b84711990f82d3cd70013e738787506a2156ebf88ac" } ] } }; // Now you can get the info content.txs.inputs.forEach(element => { console.log(element.prev_out.addr); })

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

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