为什么我的内部数组没有反序列化?

时间:2019-01-01 18:17:44

标签: c# json visual-studio-2017 json.net

我有一些要反序列化为对象的JSON。

我正在尝试这样做,但是我只得到了一些反序列化的JSON,而vin和vout JSON要么为0,要么为null。我在做什么错了?

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"
}

调试器中的输出如下:

enter image description here

我的课程如下:

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

1 个答案:

答案 0 :(得分:5)

上述对象缺少 public 属性

例如

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

表示通过反序列化初始化时将永远不会设置这些属性。

根据最初提供的代码,TransactionOutputScriptPubKey也是如此。