我正在尝试反序列化 Supreme New York JSON,但收到错误。
我使用 json2csharp.com 将 Json 转换为类。
然后我将它们全部总结为一个名为items
namespace SUPBOTTESTING
{
public class items
{
public string name { get; set; }
public int id { get; set; }
public string image_url { get; set; }
public string image_url_hi { get; set; }
public int price { get; set; }
public int sale_price { get; set; }
public bool new_item { get; set; }
public int position { get; set; }
public string category_name { get; set; }
public int price_euro { get; set; }
public int sale_price_euro { get; set; }
}
}
using System;
using System.Net;
using System.Web.Script.Serialization;
namespace SUPBOTTESTING
{
class Program
{
public static void Main()
{
{
string shop_json = new WebClient().DownloadString("https://www.supremenewyork.com/mobile_stock.json");
JavaScriptSerializer shop_object = new JavaScriptSerializer();
items[] shirt_stock = shop_object.Deserialize<items[]>(shop_json);
Console.WriteLine(shirt_stock[1]);
}
}
}
}
我收到错误:
Default constructor not found for type SUPBOTTESTING.items[]
最佳答案
好的,这就是解决方案。你的想法是正确的,但你需要了解 Json 数据的结构。
您将其反序列化
为对象
数组,而返回的 Json 数据本身不是数组或列表。它包含作为数组的子节点,因此您需要相应地构建 Object
才能成功分解数据。
这里我使用 Newtonsoft
将 Json 数据反序列化为对象。
我已经测试了代码,它返回了衬衫
的列表
static void Main(string[] args)
{
var shop_json = new WebClient().DownloadString("https://www.supremenewyork.com/mobile_stock.json");
var shirt_stock = JsonConvert.DeserializeObject<StockObject>(shop_json);
// Picking shirts to demonstrate how to display values for all shirts
var shirts = shirt_stock.products_and_categories.Shirts;
foreach (var shirt in shirts)
{
var shirtBuilder = new StringBuilder();
shirtBuilder.AppendLine($"Name: {shirt.name}");
shirtBuilder.AppendLine($"ID: {shirt.id.ToString()}");
shirtBuilder.AppendLine($"New Item: {shirt.new_item.ToString()}");
shirtBuilder.AppendLine($"Category Name: {shirt.category_name}");
Console.WriteLine(shirtBuilder);
}
}
public class StockObject
{
public ProductsCats Products_and_categories { get; set; }
}
public class ProductsCats
{
public Details[] Shirts { get; set; }
public Details[] Bags { get; set; }
public Details[] Accessories { get; set; }
public Details[] Pants { get; set; }
public Details[] Jackets { get; set; }
public Details[] Skates { get; set; }
public Details[] Hats { get; set;}
public Details[] Sweatshirts { get; set;}
[JsonProperty("Tops/Sweaters")]
public Details[] TopsSweaters { get;set;}
public Details[] New { get; set; }
}
public class Details
{
public string name { get; set; }
public int id { get; set; }
public string image_url { get; set; }
public string image_url_hi { get; set; }
public int price { get; set; }
public int sale_price { get; set; }
public bool new_item { get; set; }
public int position { get; set; }
public string category_name { get; set; }
public int price_euro { get; set; }
public int sale_price_euro { get; set; }
}
你看到我在这里做了什么吗?
那么您的 Json 数据包含一个父节点 products_and_categories
,其子节点包含一个 Shirts
数组,这就是您想要的?
StockObject
类包含名为 Products_and_categories
类型对象 ProductsCats
的 Parent
属性。
ProductsCats
对象包含 Details
类型的属性 Shirts
,它是一个数组,将在反序列化
期间使用> 过程。
希望这有帮助吗?
关于c# - 如何在 C# 中正确反序列化 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57969698/