在Visual Basic中反序列化JSON

时间:2016-02-17 13:14:00

标签: vb.net

我想反序列化这个字符串,它来自一个rest api,但我不知道该怎么做。

我知道我必须使用字符串的结构创建一个类,并且我必须使用反序列化来运行该命令。

任何人都可以帮助我吗?

由于

"
{
  "success": true,
  "data": {
    "categories": {
      "17": [
        {
          "category_id": "17",
          "name": "Software",
          "description": "",
          "sort_order": "4",
          "meta_title": "",
          "meta_description": "",
          "meta_keyword": "",
          "language_id": "1",
          "image": "http://newapi3.opencart-api.com/image/cache/catalog/categories/12316636179756542373092288841575795860377n-100x100.jpg",
          "categories": null
        },
        {
          "category_id": "17",
          "name": "Software",
          "description": "",
          "sort_order": "4",
          "meta_title": "",
          "meta_description": "",
          "meta_keyword": "",
          "language_id": "2",
          "image": "http://newapi3.opencart-api.com/image/cache/catalog/categories/12316636179756542373092288841575795860377n-100x100.jpg",
          "categories": null
        }
      ],
      "18": [
        {
          "category_id": "18",
          "name": "Laptops & Notebooks",
          "description": "<p>\r\n\tShop Laptop feature only the best laptop deals on the market. By comparing laptop deals from the likes of PC World, Comet, Dixons, The Link and Carphone Warehouse, Shop Laptop has the most comprehensive selection of laptops on the internet. At Shop Laptop, we pride ourselves on offering customers the very best laptop deals. From refurbished laptops to netbooks, Shop Laptop ensures that every laptop - in every colour, style, size and technical spec - is featured on the site at the lowest possible price.</p>\r\n",
          "sort_order": "2",
          "meta_title": "",
          "meta_description": "",
          "meta_keyword": "",
          "language_id": "1",
          "image": "http://newapi3.opencart-api.com/image/cache/catalog/demo/hp_2-100x100.jpg",
          "categories": null
        },
        {
          "category_id": "18",
          "name": "Laptops & Notebooks",
          "description": "<p>\r\n\tShop Laptop feature only the best laptop deals on the market. By comparing laptop deals from the likes of PC World, Comet, Dixons, The Link and Carphone Warehouse, Shop Laptop has the most comprehensive selection of laptops on the internet. At Shop Laptop, we pride ourselves on offering customers the very best laptop deals. From refurbished laptops to netbooks, Shop Laptop ensures that every laptop - in every colour, style, size and technical spec - is featured on the site at the lowest possible price.</p>\r\n",
          "sort_order": "2",
          "meta_title": "",
          "meta_description": "",
          "meta_keyword": "",
          "language_id": "2",
          "image": "http://newapi3.opencart-api.com/image/cache/catalog/demo/hp_2-100x100.jpg",
          "categories": null
        }
      ]
     }
  }
}"

1 个答案:

答案 0 :(得分:1)

使用this service从JSON字符串生成类。它将为您节省大量时间(并且令人头痛)。

之后,您可以考虑使用第三方库来反序列化JSON字符串。你有很多选择,但最流行和最好的选择是JSON.NET

安装完成后,您可以在反序列化JSON后继续访问所需的任何数据。

实施例

Dim data = JsonConvert.DeserializeObject(Of YourDataClass)("<YourJSONString>");
If data.Success Then
    Console.WriteLine(data.Data.Categories(0).Name);
End If