简体   繁体   中英

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. The json response is

{
"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 . By let content = JSON.parse(result)

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

to take out the info from that but it shows nothing, null. I also tried only content and it works, but it doesn't work with content.txs.inputs.addr . Can any JavaScript developers help me solve this? I'm facing this type of json for the first time.

content.txs.inputs is an array. Did you mean var data = content.txs.inputs[0].prev_out.addr; ?

You need to go down a couple more steps

inputs is an array, so you need a subscript, then two more levels of keys:

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

Note that there is an addr value in both the inputs elements. 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

  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.

  2. You are missing an closing ] in the inputs array, and you need to remove the ] at the end of the json file.

After that you can get the addr property in the next way:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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