c#从json模式转换的类中获取值



i heave json是这样的:

{
"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
}
}
}

和我使用中心类从这个网站:https://json2csharp.com/

也是我的类来反序列化json看起来像:

// 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; }
}

现在我将json反序列化成这样

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

我怎样才能得到瓦里安职业的所有变体?

我试过这样做,但是没有。

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());
}
}

我需要用变体类写行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();

或者你可以反序列化json

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

但是你必须修改你的类

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; }
}

最新更新