使用一系列项目反序列化json

时间:2015-06-20 13:24:42

标签: json vb.net

有一个Web服务返回一些像这样的json:

{
    "ContainerCount": 36,
    "ContainerUnits": "",
    "Items": [
        {
            "Id": "GBR|PR|52484481|0|0|0",
            "Text": "Google UK Ltd, 1 St. Giles High Street, London, WC2H...",
            "Highlight": "",
            "Cursor": 0,
            "Description": "",
            "Next": "Retrieve"
        },
        {
            "Id": "GBR|PR|52484493|0|0|0",
            "Text": "Google UK Ltd, Colonnade Walk, 123 Buckingham Palace Road, London, SW1W...",
            "Highlight": "",
            "Cursor": 0,
            "Description": "",
            "Next": "Retrieve"
        },
        {
            "Id": "GBR|PR|28329353|0|0|0",
            "Text": "Google UK Ltd, Belgrave House, 76 Buckingham Palace Road, London, SW1W...",
            "Highlight": "",
            "Cursor": 0,
            "Description": "",
            "Next": "Retrieve"
        },
        {
            "Id": "GBR|PR|52844784|0|0|0",
            "Text": "Google UK Ltd, 7 Pancras Square, London, N1C...",
            "Highlight": "",
            "Cursor": 0,
            "Description": "",
            "Next": "Retrieve"
        },
        {
            "Id": "GBR|PR|50573331|0|0|0",
            "Text": "Google, Peter House, Oxford Street, Manchester, M1...",
            "Highlight": "",
            "Cursor": 0,
            "Description": "",
            "Next": "Retrieve"
        },
        {
            "Id": "GBR|PR|12492688|0|0|0",
            "Text": "Johnny Goggles, 32 North John Street, Liverpool, L2...",
            "Highlight": "",
            "Cursor": 0,
            "Description": "",
            "Next": "Retrieve"
        },
        {
            "Id": "GBR|PR|12682667|0|0|0",
            "Text": "Jonny Goggles, 274 Stanley Road, Bootle, L20...",
            "Highlight": "",
            "Cursor": 0,
            "Description": "",
            "Next": "Retrieve"
        }
    ]
}

我完全不知道如何反序列化这个JSON。

最终目标是在组合框中显示元素的文本,然后使用“ID&ID”进行进一步的查询。组合框选择的元素。

1 个答案:

答案 0 :(得分:0)

创建这些类

    public class Container
    {
        public IList<Item> Items { get; set; }
    }
    public class Item
    {
        public string Id { get; set; }
        public string Text { get; set; }
    }

反序列化如下:

    var container = JsonConvert.DeserializeObject<Container>(json);

获取物品:

    container.Items