1

I have some JSON which I'd like to deserialize into Objects.

I'm trying to do it like this, but I only get some of the JSON deserialized and the vin and vout JSON are either 0s or nulls. What am I doing wrong?

Transaction t = JsonConvert.DeserializeObject<RPCResponse<Transaction>>(json).result;

JSON:

{
    "result": {
        "hex": "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0104ffffffff0100f2052a0100000043410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac00000000",
        "txid": "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098",
        "hash": "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098",
        "size": 134,
        "version": 1,
        "locktime": 0,
        "vin": [
            {
                "coinbase": "04ffff001d0104",
                "sequence": 4294967295
            }
        ],
        "vout": [
            {
                "value": 50.00000000,
                "n": 0,
                "scriptPubKey": {
                    "asm": "0496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858ee OP_CHECKSIG",
                    "hex": "410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac",
                    "reqSigs": 1,
                    "type": "pubkey",
                    "addresses": [
                        "12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX"
                    ]
                }
            }
        ],
        "blockhash": "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048",
        "confirmations": 563356,
        "time": 1231469665,
        "blocktime": 1231469665
    },
    "error": null,
    "id": "getrawtransaction"
}

The output in the debugger looks like this:

enter image description here

My Classes look like this:

public class RPCResponse<T>
{
    public T result { get; set; }
    public string error { get; set; }
    public string id { get; set; }
}


public class Transaction
{
    public string TxId { get; set; }
    public long Size { get; set; }
    public long Version { get; set; }
    public long LockTime { get; set; }
    public TransactionInputCoinbase[] Vin { get; set; }
    public TransactionOutput[] Vout { get; set; }
    public string BlockHash { get; set; }
    public long Time { get; set; }
}


public class TransactionInputCoinbase
{
    string Coinbase { get; set; }
    string Sequence { get; set; }
}

public class TransactionOutput
{
    decimal Value { get; set; }
    int N { get; set; }
    ScriptPubKey ScriptPubKey { get; set; }
}

public class ScriptPubKey
{
    string Asm { get; set; }
    string Hex { get; set; }
    long ReqSigs { get; set; }
    string Type { get; set; }
    string[] Addresses { get; set; }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Mark Allison
  • 6,838
  • 33
  • 102
  • 151
  • 5
    Could be the lack of **public** properties on the said objects... – Nkosi Jan 01 '19 at 18:23
  • 1
    So, dup of [Why are some members missing when trying to print an object by serializing to JSON?](https://stackoverflow.com/q/48156976) then? – dbc Jan 01 '19 at 18:32

1 Answers1

5

The lack of public properties on the said objects

For example

public class TransactionInputCoinbase
{
    string Coinbase { get; set; } //<--NOT PUBLIC
    string Sequence { get; set; } //<--NOT PUBLIC
}

means those properties will never be set when initialized via deserialization.

The same goes for TransactionOutput and ScriptPubKey based on the code originally provided.

Nkosi
  • 235,767
  • 35
  • 427
  • 472