从 JSON 创建数据模型

Creating datamodel from JSON

我正在开发一个将与网上商店通信的 winform 应用程序 API。当我尝试从网上商店获取所有产品时,他们向我提供了以下 JSON:

{
"code":200,
"data":{
    "product_data":{
        "122":{
            "products_id":"122",
            "products_name":"Camilla armchair",
            "products_model":"",
            "products_url":"http:\/\/mystore-demo.no\/products\/camilla-armchair",
            "products_url_identifier":"camilla-armchair",
            "products_sort_order":"0",
            "products_quantity":"0",
            "products_weight":"0",
            "products_status":"1",
            "products_price_ex_tax":"249.0000",
            "products_tax_percentage":"25",
            "products_description":"Beskrivelse Camilla armchair",
            "products_date_added":"2011-10-12 15:32:06",
            "products_last_modified":"2011-10-12 15:32:06",
            "products_brand_name":"",
            "products_brand_id":"0",
            "products_categories":[
                "42",
                "44"
            ],
            "products_attributes":[

            ],
            "products_tabs":[

            ],
            "products_images":[
                "http:\/\/mystore-demo.no\/users\/demo_mystore_no\/images\/122_Camilla_armchair_1.jpg"
            ],
            "products_index": "2"
        },
        "123":{
            "products_id":"123",
            "products_name":"Egg Chair",
            "products_model":"",
            "products_url":"http:\/\/mystore-demo.no\/products\/egg-chair",
            "products_url_identifier":"egg-chair",
            "products_sort_order":"0",
            "products_quantity":"0",
            "products_weight":"0",
            "products_status":"1",
            "products_price_ex_tax":"2999.0000",
            "products_tax_percentage":"25",
            "products_description":"Beskrivelse Egg Chair",
            "products_date_added":"2011-10-12 15:33:27",
            "products_last_modified":"2011-10-12 15:33:27",
            "products_brand_name":"",
            "products_brand_id":"0",
            "products_categories":[
                "42"
            ],
            "products_attributes":[

            ],
            "products_tabs":[

            ],
            "products_images":[
                "http:\/\/mystore-demo.no\/users\/demo_mystore_no\/images\/123_Egg_Chair_1.jpg"
            ],
            "products_index": "3"
        },
        "121":{
            "products_id":"121",
            "products_name":"Round chair",
            "products_model":"",
            "products_url":"http:\/\/mystore-demo.no\/products\/round-chair",
            "products_url_identifier":"round-chair",
            "products_sort_order":"0",
            "products_quantity":"0",
            "products_weight":"0",
            "products_status":"1",
            "products_price_ex_tax":"1599.0000",
            "products_tax_percentage":"25",
            "products_description":"Beskrivelse Round Chair",
            "products_date_added":"2011-10-11 10:43:42",
            "products_last_modified":"2011-10-11 10:43:42",
            "products_brand_name":"",
            "products_brand_id":"0",
            "products_categories":[
                "42"
            ],
            "products_attributes":[

            ],
            "products_tabs":[

            ],
            "products_images":[
                "http:\/\/mystore-demo.no\/users\/demo_mystore_no\/images\/121_Round_chair_1.jpg"
            ]
            ,
            "products_index": "1"
        }
    },
    "product_count_total":3
}

}

我创建了一个 MyStoreResponse class(数据模型),如下所示:

using Newtonsoft.Json;
namespace MyStoreSync.Models
{
public class Product
{
    [JsonProperty("products_id")]
    public string ProductsId { get; set; }

    [JsonProperty("products_name")]
    public string ProductsName { get; set; }

    [JsonProperty("products_model")]
    public string ProductsModel { get; set; }

    [JsonProperty("products_url")]
    public string ProductsUrl { get; set; }

    [JsonProperty("products_url_identifier")]
    public string ProductsUrlIdentifier { get; set; }

    [JsonProperty("products_sort_order")]
    public string ProductsSortOrder { get; set; }

    [JsonProperty("products_quantity")]
    public string ProductsQuantity { get; set; }

    [JsonProperty("products_weight")]
    public string ProductsWeight { get; set; }

    [JsonProperty("products_status")]
    public string ProductsStatus { get; set; }

    [JsonProperty("products_price_ex_tax")]
    public string ProductsPriceExTax { get; set; }

    [JsonProperty("products_tax_percentage")]
    public string ProductsTaxPercentage { get; set; }

    [JsonProperty("products_description")]
    public string ProductsDescription { get; set; }

    [JsonProperty("products_date_added")]
    public string ProductsDateAdded { get; set; }

    [JsonProperty("products_last_modified")]
    public string ProductsLastModified { get; set; }

    [JsonProperty("products_brand_name")]
    public string ProductsBrandName { get; set; }

    [JsonProperty("products_brand_id")]
    public string ProductsBrandId { get; set; }

    [JsonProperty("products_categories")]
    public string[] ProductsCategories { get; set; }

    [JsonProperty("products_attributes")]
    public object[] ProductsAttributes { get; set; }

    [JsonProperty("products_tabs")]
    public object[] ProductsTabs { get; set; }

    [JsonProperty("products_images")]
    public string[] ProductsImages { get; set; }

    [JsonProperty("products_index")]
    public string ProductsIndex { get; set; }
}

public class ProductData
{
    [JsonProperty("Product")]
    public Product Product { get; set; }
}

public class Data
{
    [JsonProperty("product_data")]
    public ProductData ProductData { get; set; }

    [JsonProperty("product_count_total")]
    public int ProductCountTotal { get; set; }
}

public class MyStoreResponse
{
    [JsonProperty("code")]
    public int Code { get; set; }

    [JsonProperty("data")]
    public Data Data { get; set; }
}

}

我得到了正确的 ProductCountTotal,但 Product 始终为空。对解析 JSON 有更多了解的人可以帮助我吗?

您遇到此问题是因为 "product" 不是返回的 json 中的键,在您的 ProductData class 中,键名为 "product",但 "121""122""123" 代表产品...

您可以像这样进行产品序列化:

foreach(var x in productDataInstance)
{
    Product p = JsonConvert.Deserialize(x);
}

此外,我认为 ProductData 应该有一个产品列表。

public class ProductData
{
    public List<Product> products = new List<Products>();
}

我们现在有:

foreach(var x in productDataInstance)
{
    Product p = JsonConvert.Deserialize(x);
    productDataInstance.products.Add(p);
}

注意 productDataInstance 在您的 Data class 中是 ProductData

希望对您有所帮助。

您需要将 ProductData 属性 设为字典:

public class Data
{
    [JsonProperty("product_data")]
    public Dictionary<string, Product> ProductData { get; set; }

    [JsonProperty("product_count_total")]
    public int ProductCountTotal { get; set; }
}

ProductData class 是不必要的。 JSON 属性 名称 "122""123""121" 成为字典键,关联的 Product 成为值。参见 Serialize a Dictionary