Home > Mobile >  C# get value from class earlier converted class from json schema
C# get value from class earlier converted class from json schema

Time:06-25

i heave json looks like this :

  {
  "status": "SUCCESS",
  "storage_id": "bl_1",
  "products": {
    "1055199655": {
      "product_id": 1055199655,
      "ean": "0888411571650",
      "sku": "924593-001",
      "name": "Nike Metcon 4 Black 924593-001 Damskie ",
      "quantity": 3,
      "price_netto": 269,
      "price_brutto": 269,
      "price_wholesale_netto": 0,
      "tax_rate": 0,
      "weight": 0,
      "man_name": "Nike",
      "man_image": null,
      "category_id": 1067613,
      "images": [
        "https://upload.cdn.baselinker.com/products/21606/1055199655.jpg",
        "https://upload.cdn.baselinker.com/products/21606/1055199655_1.jpg",
        "https://upload.cdn.baselinker.com/products/21606/1055199655_2.jpg",
        "https://upload.cdn.baselinker.com/products/21606/1055199655_3.jpg",
        "https://upload.cdn.baselinker.com/products/21606/1055199655_4.jpg",
        "https://upload.cdn.baselinker.com/products/21606/1055199655_5.jpg",
        "https://upload.cdn.baselinker.com/products/21606/1055199655_6.jpg",
        "https://upload.cdn.baselinker.com/products/21606/1055199655_7.jpg"
      ],
      "features": [
        [
          "Płeć",
          "Produkt damski"
        ],
        [
          "Model",
          "Metcon 4"
        ],
        [
          "Kolor podeszwy",
          "biały"
        ],
        [
          "Kod producenta",
          "924593-001"
        ],
        [
          "Wysokość",
          "niskie"
        ],
        [
          "Marka",
          "Nike"
        ],
        [
          "ebay",
          "Metcon 4 Black"
        ]
      ],
      "variants": [
        {
          "variant_id": 1055308982,
          "name": "Nike Metcon 4 Black 924593-001 Damskie  38.5",
          "price": 269,
          "quantity": 1,
          "sku": "",
          "ean": "0888411571650"
        },
        {
          "variant_id": 1055308983,
          "name": "Nike Metcon 4 Black 924593-001 Damskie  38",
          "price": 0,
          "quantity": 0,
          "sku": "",
          "ean": "0888411571650"
        },
        {
          "variant_id": 1055308984,
          "name": "Nike Metcon 4 Black 924593-001 Damskie  37.5",
          "price": 269,
          "quantity": 1,
          "sku": "",
          "ean": "0888411571650"
        },
        {
          "variant_id": 1055308985,
          "name": "Nike Metcon 4 Black 924593-001 Damskie  36.5",
          "price": 0,
          "quantity": 0,
          "sku": "",
          "ean": "0888411571650"
        },
        {
          "variant_id": 1055308986,
          "name": "Nike Metcon 4 Black 924593-001 Damskie  36",
          "price": 269,
          "quantity": 1,
          "sku": "",
          "ean": "0888411571650"
        }
      ],
      "description": null,
      "description_extra1": null,
      "description_extra2": null,
      "description_extra3": null,
      "description_extra4": null
    },
    "1055641817": {
      "product_id": 1055641817,
      "ean": "",
      "sku": "",
      "name": "NOWE BUTY VANS OLD SKOOL NOWE BUTY VANS OLD SKOOL BLACK/WHITE ORYGINALNE",
      "quantity": 0,
      "price_netto": 0,
      "price_brutto": 0,
      "price_wholesale_netto": 0,
      "tax_rate": 23,
      "weight": 0,
      "man_name": "Vans",
      "man_image": null,
      "category_id": 1845617,
      "images": [],
      "features": [],
      "variants": [
        {
          "variant_id": 1055710066,
          "name": "NOWE BUTY VANS OLD SKOOL NOWE BUTY VANS OLD SKOOL BLACK/WHITE ORYGINALNE 42",
          "price": 599.99,
          "quantity": 0,
          "sku": "",
          "ean": "29193201490"
        }
      ],
      "description": null,
      "description_extra1": null,
      "description_extra2": null,
      "description_extra3": null,
      "description_extra4": null
    }
  }
}

And i use cenverter to classes from this website : https://json2csharp.com/

also my classes to deserialize json looks like :

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class _1055199655
{
    public int product_id { get; set; }
    public string ean { get; set; }
    public string sku { get; set; }
    public string name { get; set; }
    public int quantity { get; set; }
    public int price_netto { get; set; }
    public int price_brutto { get; set; }
    public int price_wholesale_netto { get; set; }
    public int tax_rate { get; set; }
    public int weight { get; set; }
    public string man_name { get; set; }
    public object man_image { get; set; }
    public int category_id { get; set; }
    public List<string> images { get; set; }
    public List<List<string>> features { get; set; }
    public List<Variant> variants { get; set; }
    public object description { get; set; }
    public object description_extra1 { get; set; }
    public object description_extra2 { get; set; }
    public object description_extra3 { get; set; }
    public object description_extra4 { get; set; }
}

public class _1055641817
{
    public int product_id { get; set; }
    public string ean { get; set; }
    public string sku { get; set; }
    public string name { get; set; }
    public int quantity { get; set; }
    public int price_netto { get; set; }
    public int price_brutto { get; set; }
    public int price_wholesale_netto { get; set; }
    public int tax_rate { get; set; }
    public int weight { get; set; }
    public string man_name { get; set; }
    public object man_image { get; set; }
    public int category_id { get; set; }
    public List<object> images { get; set; }
    public List<object> features { get; set; }
    public List<Variant> variants { get; set; }
    public object description { get; set; }
    public object description_extra1 { get; set; }
    public object description_extra2 { get; set; }
    public object description_extra3 { get; set; }
    public object description_extra4 { get; set; }
}

public class Products
{
    public _1055199655 _1055199655 { get; set; }
    public _1055641817 _1055641817 { get; set; }
}

public class Root
{
    public string status { get; set; }
    public string storage_id { get; set; }
    public Products products { get; set; }
}

public class Variant
{
    public int variant_id { get; set; }
    public string name { get; set; }
    public int price { get; set; }
    public int quantity { get; set; }
    public string sku { get; set; }
    public string ean { get; set; }
}

and now i deserialize this json like this :

 string json = getProductsData();

            Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(json);

How can i get all variants from Varian class?

I tried something like this but there is nothing.

  if(myDeserializedClass.status == "SUCCESS")
            {
                Console.WriteLine("Jest suckces...");
                List<Variant> myClassList = new List<Variant>();

                foreach (var inventoryItem in myClassList)
                {
                    Console.WriteLine("EAN..."   inventoryItem.ean.ToString());
                }

            }

I need to writeline Eans with Variant class .Can someone help me with that? Maybe there is something to change in class or something.

CodePudding user response:

you don't classes to get list of ean

var products = JObject.Parse(json); 

if(products["status"] == "SUCCESS")

   List<string> eans =   ( (JObject) products["products"]).Properties()
                     .Select(x => ((JObject) x.Value).Properties().Where(x=>x.Name=="variants")
                     .Select(x=> x.Value).First())
                     .Select(x=>x.Select(x=>(string) x["ean"]))
                     .SelectMany(x=>x)
                     .ToList();

or you can deserialize your json

Root products = JsonConvert.DeserializeObject<Root>(json);

List<string> eans = products.products
                       .SelectMany(x =>  x.Value.variants)
                       .Select(x => x.ean)
                       .ToList();

but you will have to fix your classes

public class Root
{
    public string status { get; set; }
    public string storage_id { get; set; }
    public Dictionary<string, Product> products { get; set; }
}
public class Product
{
    public int product_id { get; set; }
    public string ean { get; set; }
    public string sku { get; set; }
    public string name { get; set; }
    public int quantity { get; set; }
    public int price_netto { get; set; }
    public int price_brutto { get; set; }
    public int price_wholesale_netto { get; set; }
    public int tax_rate { get; set; }
    public int weight { get; set; }
    public string man_name { get; set; }
    public object man_image { get; set; }
    public int category_id { get; set; }
    public List<string> images { get; set; }
    public List<List<string>> features { get; set; }
    public List<Variant> variants { get; set; }
    public object description { get; set; }
    public object description_extra1 { get; set; }
    public object description_extra2 { get; set; }
    public object description_extra3 { get; set; }
    public object description_extra4 { get; set; }
}
public class Variant
{
    public int variant_id { get; set; }
    public string name { get; set; }
    public double price { get; set; }
    public int quantity { get; set; }
    public string sku { get; set; }
    public string ean { get; set; }
}
  • Related