首页 > 解决方案 > 如何将 JSON 输出转换为 c# 对象并将它们存储到数组中

问题描述

我正在尝试将 JSON 输出反序列化为 .net 对象,但将输出设为 null。

我编写了这段代码来获取 JSON 数据并对其进行反序列化。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using Newtonsoft.Json;
namespace ConsoleApp1
{
    public enum httpverb
    {
        GET,
        POST,
        PUT,
        DELETE
    }
    public class Item
    {
        public string item_id { get; set; }
        public string name { get; set; }
        public string item_name { get; set; }
        public string hsn_or_sac { get; set; }
        public string sku { get; set; }
        public string image_name { get; set; }
        public string status { get; set; }
        public string source { get; set; }
        public string description { get; set; }       
        public string cf_category { get; set; }
        public string cf_maximum_retail_price_mrp { get; set; }
        public DateTime created_time { get; set; }
        public DateTime last_modified_time { get; set; }
        public bool track_serial_number { get; set; }
    }
    class APIs
    {
         xxxxxcode goes here to fetch JSON from API and store into variable strResponseValue
         string Jsoncontent = strResponseValue;   //Jsoncontent holds json data
         Item books = JsonConvert.DeserializeObject<Item>(Jsoncontent);
         //how to access each object.
}
}

这是必须反序列化的 JSON 输出。

  {
    "code": 0,
    "message": "success",
    "items": [
        {
            "item_id": "519558000025988777",
            "name": "0",
            "image_document_id": "",
            "item_name": "0",
            "hsn_or_sac": "",
            "sku": "",
            "image_name": "",
            "status": "active",
            "source": "user",
            "is_linked_with_zohocrm": false,
            "zcrm_product_id": "",
            "description": "",
            "item_tax_preferences": [
                {
                    "tax_specification": "inter",
                    "tax_type": 0,
                    "tax_name": "IGST18",
                    "tax_percentage": 18,
                    "tax_id": "519558000000076071"
                },
                {
                    "tax_specification": "intra",
                    "tax_type": 2,
                    "tax_name": "GST18",
                    "tax_percentage": 18,
                    "tax_id": "519558000000076077"
                }
            ],
            "rate": 1439.84,
            "tax_id": "",
            "reorder_level": "",
            "tax_name": "",
            "tax_percentage": 0,
            "purchase_account_id": "",
            "purchase_account_name": "",
            "account_name": "Sales",
            "unit": "",
            "purchase_description": "",
            "purchase_rate": 0,
            "item_type": "sales",
            "product_type": "goods",
            "is_taxable": true,
            "tax_exemption_id": "",
            "tax_exemption_code": "",
            "has_attachment": false,
            "is_combo_product": false,
            "created_time": "2019-04-25T17:31:13+0530",
            "last_modified_time": "2019-04-25T17:31:13+0530",
            "track_serial_number": false
        },
        {
            "item_id": "519558000005300339",
            "name": "Acer HA270 27\" Full HD IPS White Color Ultra Slim Monitor",
            "image_document_id": "",
            "item_name": "Acer HA270 27\" Full HD IPS White Color Ultra Slim Monitor",
            "hsn_or_sac": "8528",
            "sku": "HA270",
            "image_name": "",
            "status": "active",
            "source": "user",
            "is_linked_with_zohocrm": false,
            "zcrm_product_id": "",
            "description": "",
            "item_tax_preferences": [
                {
                    "tax_specification": "inter",
                    "tax_type": 0,
                    "tax_name": "IGST18",
                    "tax_percentage": 18,
                    "tax_id": "519558000000076071"
                },
                {
                    "tax_specification": "intra",
                    "tax_type": 2,
                    "tax_name": "GST18",
                    "tax_percentage": 18,
                    "tax_id": "519558000000076077"
                }
            ],
            "rate": 16999,
            "tax_id": "",
            "reorder_level": "",
            "tax_name": "",
            "tax_percentage": 0,
            "purchase_account_id": "519558000000043003",
            "purchase_account_name": "Cost of Goods Sold",
            "account_name": "Sales",
            "unit": "Unit",
            "purchase_description": "",
            "purchase_rate": 11300,
            "item_type": "inventory",
            "product_type": "goods",
            "is_taxable": true,
            "tax_exemption_id": "",
            "tax_exemption_code": "",
            "stock_on_hand": -1,
            "has_attachment": false,
            "is_combo_product": false,
            "cf_category": "Monitor",
            "cf_maximum_retail_price_mrp": "18500",
            "created_time": "2019-02-13T18:08:37+0530",
            "last_modified_time": "2019-04-28T16:16:11+0530",
            "track_serial_number": false
        },
        {
            "item_id": "519558000001595908",
            "name": "Acer KA240H bd 24-inch Full HD (1920 x 1080) Display (VGA, DVI Ports)",
            "image_document_id": "",
            "item_name": "Acer KA240H bd 24-inch Full HD (1920 x 1080) Display (VGA, DVI Ports)",
            "hsn_or_sac": "8528",
            "sku": "KA240H",
            "image_name": "",
            "status": "active",
            "source": "user",
            "is_linked_with_zohocrm": false,
            "zcrm_product_id": "",
            "description": "",
            "item_tax_preferences": [
                {
                    "tax_specification": "inter",
                    "tax_type": 0,
                    "tax_name": "IGST18",
                    "tax_percentage": 18,
                    "tax_id": "519558000000076071"
                },
                {
                    "tax_specification": "intra",
                    "tax_type": 2,
                    "tax_name": "GST18",
                    "tax_percentage": 18,
                    "tax_id": "519558000000076077"
                }
            ],
            "rate": 9000,
            "tax_id": "",
            "reorder_level": "",
            "tax_name": "",
            "tax_percentage": 0,
            "purchase_account_id": "519558000000043003",
            "purchase_account_name": "Cost of Goods Sold",
            "account_name": "Sales",
            "unit": "Unit",
            "purchase_description": "",
            "purchase_rate": 6600,
            "item_type": "inventory",
            "product_type": "goods",
            "is_taxable": true,
            "tax_exemption_id": "",
            "tax_exemption_code": "",
            "stock_on_hand": 2,
            "has_attachment": false,
            "is_combo_product": false,
            "cf_category": "Monitor",
            "cf_maximum_retail_price_mrp": "0",
            "created_time": "2018-08-31T12:53:35+0530",
            "last_modified_time": "2018-12-13T21:01:59+0530",
            "track_serial_number": false
        },
        {
            "item_id": "519558000005612779",
            "name": "ADATA XPG GAMMIX D10 8GB DDR4 RAM 2666MHz for Desktop RAM",
            "image_document_id": "",
            "item_name": "ADATA XPG GAMMIX D10 8GB DDR4 RAM 2666MHz for Desktop RAM",
            "hsn_or_sac": "8473",
            "sku": "AX4U266638G16-SRG",
            "image_name": "",
            "status": "active",
            "source": "user",
            "is_linked_with_zohocrm": false,
            "zcrm_product_id": "",
            "description": "",
            "item_tax_preferences": [
                {
                    "tax_specification": "inter",
                    "tax_type": 0,
                    "tax_name": "IGST18",
                    "tax_percentage": 18,
                    "tax_id": "519558000000076071"
                },
                {
                    "tax_specification": "intra",
                    "tax_type": 2,
                    "tax_name": "GST18",
                    "tax_percentage": 18,
                    "tax_id": "519558000000076077"
                }
            ],
            "rate": 4999,
            "tax_id": "",
            "reorder_level": "",
            "tax_name": "",
            "tax_percentage": 0,
            "purchase_account_id": "519558000000043003",
            "purchase_account_name": "Cost of Goods Sold",
            "account_name": "Sales",
            "unit": "Unit",
            "purchase_description": "",
            "purchase_rate": 3100,
            "item_type": "inventory",
            "product_type": "goods",
            "is_taxable": true,
            "tax_exemption_id": "",
            "tax_exemption_code": "",
            "stock_on_hand": 2,
            "has_attachment": false,
            "is_combo_product": false,
            "cf_category": "RAM",
            "cf_maximum_retail_price_mrp": "14999",
            "created_time": "2019-03-01T18:06:05+0530",
            "last_modified_time": "2019-04-27T17:08:53+0530",
            "track_serial_number": false
        }
    ],
    "page_context": {
        "page": 1,
        "per_page": 200,
        "has_more_page": true,
        "report_name": "Items",
        "applied_filter": "Status.All",
        "custom_fields": [],
        "sort_column": "name",
        "sort_order": "A"
    }
}

提供的 JSON 的输出必须反序列化为 C# 对象并存储到数组中。

标签: c#json.net

解决方案


您必须在代码中添加此根类:

    public class Root
    {
        public string code { get; set; }
        public string message { get; set; }
        public Item[] items { get; set; }
    }

然后在您已经存在的Item类中添加或删除所需的属性。例如,在输入 JSON 中,属性比您的类中的要多,如果您需要更多,只需将它们添加到您的 Item 类中。我使用了您提供的课程。

然后你这样做:

var inputObj = JsonConvert.DeserializeObject<Root>(json);
  • 您需要以下 NuGet 包:Newtonsoft.Json

  • 上面代码中的json是保存整个 JSON 的字符串变量,由您提供。请注意,在您提供的 json 中,最后缺少“}”。

inputObj是 Root 类类型的对象,包含所有项目的数组。例如 inputObj.items

在此处输入图像描述

如果您需要更多帮助,请在下方评论。

干杯


推荐阅读