反序列化对象的Json列表c#

时间:2016-04-02 02:35:10

标签: c# json serialization deserialization

好的,所以我不能完全确定如何解释这个,但生病了,尽我所能。我从json处理单个对象的反序列化,但是当我得到json形式的对象列表时,它不起作用,并且在对象列表中时,在单个对象之外还有一些额外的细节。

代码行我很确定问题是

var model = JsonConvert.DeserializeObject<DeserializedObjects.BlockList>(JObject.Parse(json).ToString());

但我无法弄清楚如何解决它。

反正。

这是json中的多个数据对象来自:

    public static async Task<DeserializedObjects.BlockList> GetUpToTenBlocks(int height)
    {

        var JData = (dynamic)new JObject();
        JData.height = height;
        String uri = String.Concat(partialApi, "/local/chain/blocks-after");

        var response = await client.PostAsync(uri, new StringContent(JData.ToString(), Encoding.UTF8, "application/json"));
        var content = response.Content;
        {
            var json = await content.ReadAsStringAsync();

            var model = JsonConvert.DeserializeObject<DeserializedObjects.BlockList>(JObject.Parse(json).ToString());

            Console.WriteLine(model.AtIndex[1].difficulty);
            return model;
        }
    }

被反序列化为:

 public class PrevBlockHash
    {
        public string data { get; set; }
    }

    public class Block
    {
        public int timeStamp { get; set; }
        public string signature { get; set; }
        public PrevBlockHash prevBlockHash { get; set; }
        public int type { get; set; }
        public List<object> transactions { get; set; }
        public int version { get; set; }
        public string signer { get; set; }
        public long height { get; set; }
    }

    public class Datum
    {
        public object difficulty { get; set; }
        public List<object> txes { get; set; }
        public Block block { get; set; }
        public string hash { get; set; }
    }

    public class BlockList
    {
        public List<Datum> AtIndex { get; set; }
    }

这是json有效载荷:

{
  "data": [
    {
      "difficulty": 11763927507942,
      "txes": [],
      "block": {
        "timeStamp": 167479,
        "signature": "bb062d9b5f132b39b9e56de2413bf04928af009587446621da7afd351d
15a2ce7b5504450acf41bc3b19ab71e9bf34722005239d93f05a2318130f85118df40c",
        "prevBlockHash": {
          "data": "d4875ad2fc74dacfa89a13f24159d14555d3766f4fe2d708a7596f84eba88
31b"
        },
        "type": 1,
        "transactions": [],
        "version": 1744830465,
        "signer": "00a30788dc1f042da959309639a884d8f6a87086cda10300d2a7c3a0e0891
a4d",
        "height": 1001
      },
      "hash": "f70898011d7343a0823de9c9cf263de29ddf2c16bb78cea626b9af90ea7ec260"

    },
    {
      "difficulty": 11625594628802,
      "txes": [],
      "block": {
        "timeStamp": 167561,
        "signature": "116dedf43dd06b9ca634db0e20e06cc93337cdba155bced4d843ece4cc
9a57487d58e9a34d8a0e19bf71d3b7facb15179a87767f0063ebbce7c940cd545d5f01",
        "prevBlockHash": {
          "data": "f70898011d7343a0823de9c9cf263de29ddf2c16bb78cea626b9af90ea7ec
260"
        },
        "type": 1,
        "transactions": [],
        "version": 1744830465,
        "signer": "6ecd181da287c9ccb0075336de36427f25cbc216dc6b1f0e87e35e41a39f6
3fe",
        "height": 1002
      },
      "hash": "77b5644c35e0d0d51f8bb967d0d92e0ddb03c4ede6632cb3b7651b7394617562"

    },
    {
      "difficulty": 11538802895169,
      "txes": [],
      "block": {
        "timeStamp": 167624,
        "signature": "982574132fdc99b6f484acdd3f1cb5229b2bf78ad7b4e9af3d7a1873da
b987401f8bf808ff749aca70c503f490db1411b6cd89dbb0c1daa24fd580f91d3d9601",
        "prevBlockHash": {
          "data": "77b5644c35e0d0d51f8bb967d0d92e0ddb03c4ede6632cb3b7651b7394617
562"
        },
        "type": 1,
        "transactions": [],
        "version": 1744830465,
        "signer": "26a3ac4b24647c77dc87780a95e50cb8d7744966e4569e3ac24e52c532c0c
d0d",
        "height": 1003
      },
      "hash": "1a6d52c6317150d1839790da2c1481d714038c869842f769affbec0fdeec9861"

    }
  ]
}

1 个答案:

答案 0 :(得分:1)

试试这个:

var model = JsonConvert.DeserializeObject<DeserializedObjects.BlockList>(json);
Console.WriteLine(model.data[1].difficulty);

同时:

public class BlockList
{
    public List<Datum> data { get; set; }
}