简体   繁体   中英

index for jObject properties not working c#

I am parsing a Bitcoin transaction String into a JObject (it is in JSON format) and want to reference the prev_out hash and second value in the JSON String (see below for an example). As you can see below I have tried to get the property values using the index but I keep getting null back. Please help!

What the Typical JSON string looks like:

{
    "hash" : "4ebf7f7ca0a5dafd10b9bd74d8cb93a6eb0831bcb637fec8e8aabf842f1c2688",
    "ver" : 1,
    "vin_sz" : 1,
    "vout_sz" : 2,
    "lock_time" : 0,
    "size" : 225,
    "in" : [{
            "prev_out" : {
                "hash" : "bf7d91ac70917f98b497927e1b07267507652b206df14ecdba2e9390b9bffc65",
                "n" : 0
            },
            "scriptSig" :
            "                               3044022069b6b0f1a8d453bdb89e3ad475232b8e01d2851e7b53acab3f830f40e80b3b5102203c0   49
                    867975360020293c735d48b4a2dda003aa781c1d8ccd2c7af290dcd11de01
                    02e3538427350039e67ea99e935cefb740badf3d09ebc301b0bc9d1bb0301a3417"
        }
    ],
    "out" : [{
            "value" : "0.08990000",
            "scriptPubKey" : "OP_DUP OP_HASH160 5b1d720daf0e95e37d0eaedd282b6ed9a40bab71
                     OP_EQUALVERIFY OP_CHECKSIG"
        }, {
            "value" : "0.01000000",
            "scriptPubKey" : "OP_DUP OP_HASH160 71049fd47ba2107db70d53b127cae4ff0a37b4ab
                    OP_EQUALVERIFY OP_CHECKSIG"
        }
    ]
}

I am trying to reference the prev_out hash value using this:

JObject transaction = JObject.Parse(t1);
var d = transaction["in"][0]["hash"];

Where t1 is the JSON transaction string

I try to get the second value property using this:

JObject v = JObject.Parse(t1);
var val = v["out"][1]; //second value starting from 0
value = val.ToString();

"hash" lives inside "prev_out", so you need to access it like this:

var d = transaction["in"][0]["prev_out"]["hash"];

This becomes clear if you format the JSON.

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