我正在尝试使用flipkart API获取产品的详细信息

时间:2015-04-24 05:32:23

标签: c# asp.net json json.net flipkart-api

我无法将JSON响应正确转换为类,然后以字符串形式显示。

这是回复:

    {
    "productBaseInfo": {
        "productIdentifier": {
            "productId": "SHODRYT32JN5N6WY",
            "categoryPaths": {
                "categoryPath": [
                    [
                        {
                            "title": "Footwear"
                        },
                        {
                            "title": "Men's Footwear"
                        },
                        {
                            "title": "Shoes"
                        },
                        {
                            "title": "Casual Shoes"
                        }
                    ]
                ]
            }
        },
        "productAttributes": {
            "title": "Diesel Magnete Exposure I Boots",
            "productDescription": null,
            "imageUrls": {
                "400x400": "url",
                "75x75": "url",
                "700x700": "url",
                "275x275": "url",
                "125x125": "url",
                "40x40": "url",
                "100x100": "url",
                "200x200": "url",
                "unknown": "http://img5a.flixcart.com/image/shoe/6/w/y/t7434-magnete-exposure-i-sneaker-mid-diesel-43-original-imadtxfxcgzkbzbg.jpeg",
                "180x240": "url",
                "275x340": "url"
            },
            "maximumRetailPrice": {
                "amount": 15800,
                "currency": "INR"
            },
            "sellingPrice": {
                "amount": 15800,
                "currency": "INR"
            },
            "productUrl": "http://dl.flipkart.com/dl/diesel-magnete-exposure-boots/p/itmdryt4zdzbwehx?pid=SHODRYT32JN5N6WY&affid=keviv2809",
            "productBrand": "Diesel",
            "inStock": true,
            "codAvailable": true,
            "emiAvailable": false,
            "discountPercentage": 0,
            "cashBack": null,
            "offers": [
                {
                    "title": "Get Flat 60% Off on this product"
                }
            ],
            "size": "44",
            "color": "Grey",
            "sizeUnit": "Euro",
            "sizeVariants": "[SHODRYT33YFFZHTU, SHODRYT3BZGFSBNV, SHODRYT3QECRQZ8Y, SHODRYT3QPQZG4XF, SHODRYT3Y5SUNHRM]",
            "colorVariants": "[SHODRYT3MVHPHVS2, SHODVN2YP5AXADT9, SHODVN2YUGUUGUST]",
            "styleCode": "MAGNETE EXPOSURE I - sneaker mid"
        }
    },
    "productShippingBaseInfo": {
        "shippingOptions": [
            {
                "estimatedDelivery": 3,
                "deliveryTimeUnits": "DAYS",
                "shippingType": "REGULAR"
            },
            {
                "estimatedDelivery": 1,
                "deliveryTimeUnits": "DAYS",
                "shippingType": "REGULAR"
            },
            {
                "estimatedDelivery": 0,
                "deliveryTimeUnits": "DAYS",
                "shippingType": "REGULAR"
            }
        ]
    },
    "offset": "v1:osp-cil-nit-e1f:SHODRYT32JN5N6WY"
}

我使用的课程是:

public class CategoryPaths
{
    public List<ProductIdentifier> categoryPath { get; set; }
}

public class ProductIdentifier
{
    public string productId { get; set; }
    public CategoryPaths categoryPaths { get; set; }
}

public class ImageUrls
{
    public string 400x400 { get; set; }
    public string 125x167 { get; set; }
    public string 75x75 { get; set; }
    public string 700x700 { get; set; }
    public string 275x275 { get; set; }
    public string 125x125 { get; set; }
    public string 40x40 { get; set; }
    public string 100x100 { get; set; }
    public string 200x200 { get; set; }
    public string 1100x1360 { get; set; }
    public string unknown { get; set; }
    public string 180x240 { get; set; }
    public string 275x340 { get; set; }
}

public class MaximumRetailPrice
{
    public double amount { get; set; }
    public string currency { get; set; }
}

public class SellingPrice
{
    public double amount { get; set; }
    public string currency { get; set; }
}

public class Offer
{
    public string title { get; set; }
}

public class ProductAttributes
{
    public string title { get; set; }
    public object productDescription { get; set; }
    public ImageUrls imageUrls { get; set; }
    public MaximumRetailPrice maximumRetailPrice { get; set; }
    public SellingPrice sellingPrice { get; set; }
    public string productUrl { get; set; }
    public string productBrand { get; set; }
    public bool inStock { get; set; }
    public bool codAvailable { get; set; }
    public bool emiAvailable { get; set; }
    public double discountPercentage { get; set; }
    public object cashBack { get; set; }
    public List<Offer> offers { get; set; }
    public string size { get; set; }
    public string color { get; set; }
    public string sizeUnit { get; set; }
    public string sizeVariants { get; set; }
    public string colorVariants { get; set; }
    public string styleCode { get; set; }
}

public class ProductBaseInfo
{
    public ProductIdentifier productIdentifier { get; set; }
    public ProductAttributes productAttributes { get; set; }
}

public class ShippingOption
{
    public int estimatedDelivery { get; set; }
    public string deliveryTimeUnits { get; set; }
    public string shippingType { get; set; }
}

public class ProductShippingBaseInfo
{
    public List<ShippingOption> shippingOptions { get; set; }
}

public class RootObject
{
    public ProductBaseInfo productBaseInfo { get; set; }
    public ProductShippingBaseInfo productShippingBaseInfo { get; set; }
    public string offset { get; set; }
}

我使用以下代码对其进行反序列化,然后使用foreach循环来显示它。

CategoryPaths pi = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<CategoryPaths>(json);

我收到以下错误:

  

对象引用未设置为对象的实例。

我认为我在使用Deserialize(方法)时出错了 有人请帮忙

[编辑]:我现在通过使用foll代码得到了Deserialize方法:

var dynJson = JsonConvert.DeserializeObject<RootObject>(json); Label1.Text = dynJson.productBaseInfo.productAttributes.maximumRetailPrice.amount.ToString()

我无法使用值&#34; 400X400&#34;:&#34; url&#34;存储json响应。在字符串中,因为它不允许我使用名称400X400

声明字符串变量

1 个答案:

答案 0 :(得分:0)

定义的类型与Json结构不匹配。您可以使用以下代码动态反序列化并了解结构

using System.Web.Script.Serialization;

string json = "{\"productBaseInfo\":{\"productIdentifier\":{\"productId\":\"SHODRYT32JN5N6WY\",\"categoryPaths\":{\"categoryPath\":[[{\"title\":\"Footwear\"},{\"title\":\"Men's Footwear\"},{\"title\":\"Shoes\"},{\"title\":\"Casual Shoes\"}]]}},\"productAttributes\":{\"title\":\"Diesel Magnete Exposure I Boots\",\"productDescription\":null,\"imageUrls\":{\"400x400\":\"\",\"url\":\"url\",\"75x75\":\"url\",\"700x700\":\"url\",\"275x275\":\"url\",\"125x125\":\"url\",\"40x40\":\"url\",\"100x100\":\"url\",\"200x200\":\"\",\"url\":\"url\",\"unknown\":\"http://img5a.flixcart.com/image/shoe/6/w/y/t7434-magnete-exposure-i-sneaker-mid-diesel-43-original-imadtxfxcgzkbzbg.jpeg\",\"180x240\":\"url\",\"275x340\":\"url\"},\"maximumRetailPrice\":{\"amount\":15800.0,\"currency\":\"INR\"},\"sellingPrice\":{\"amount\":15800.0,\"currency\":\"INR\"},\"productUrl\":\"http://dl.flipkart.com/dl/diesel-magnete-exposure-boots/p/itmdryt4zdzbwehx?pid=SHODRYT32JN5N6WY&affid=keviv2809\",\"productBrand\":\"Diesel\",\"inStock\":true,\"codAvailable\":true,\"emiAvailable\":false,\"discountPercentage\":0.0,\"cashBack\":null,\"offers\":[{\"title\":\"Get Flat 60% Off on this product\"}],\"size\":\"44\",\"color\":\"Grey\",\"sizeUnit\":\"Euro\",\"sizeVariants\":\"[SHODRYT33YFFZHTU, SHODRYT3BZGFSBNV, SHODRYT3QECRQZ8Y, SHODRYT3QPQZG4XF, SHODRYT3Y5SUNHRM]\",\"colorVariants\":\"[SHODRYT3MVHPHVS2, SHODVN2YP5AXADT9, SHODVN2YUGUUGUST]\",\"styleCode\":\"MAGNETE EXPOSURE I - sneaker mid\"}},\"productShippingBaseInfo\":{\"shippingOptions\":[{\"estimatedDelivery\":3,\"deliveryTimeUnits\":\"DAYS\",\"shippingType\":\"REGULAR\"},{\"estimatedDelivery\":1,\"deliveryTimeUnits\":\"DAYS\",\"shippingType\":\"REGULAR\"},{\"estimatedDelivery\":0,\"deliveryTimeUnits\":\"DAYS\",\"shippingType\":\"REGULAR\"}]},\"offset\":\"v1:osp-cil-nit-e1f:SHODRYT32JN5N6WY\"}";

dynamic value = new JavaScriptSerializer().DeserializeObject(json);