代码之家  ›  专栏  ›  技术社区  ›  Mark Allison

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

  •  1
  • Mark Allison  · 技术社区  · 7 年前

    我有一些JSON,我想反序列化为对象。

    我试着这样做,但我只得到了一些JSON反序列化,并且vin和vout JSON不是0就是空。我做错了什么?

    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 回复  |  直到 7 年前
        1
  •  5
  •   Nkosi    7 年前

    缺乏 平民的 所述对象上的属性

    例如

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

    意味着当通过反序列化初始化时,将永远不会设置这些属性。

    同样的道理也适用于 TransactionOutput ScriptPubKey 基于最初提供的代码。