Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中正确反序列化JSON#_C#_System.web - Fatal编程技术网

C# 如何在C中正确反序列化JSON#

C# 如何在C中正确反序列化JSON#,c#,system.web,C#,System.web,我试图反序列化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

我试图反序列化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; }
    }
}
使用系统;
Net系统;
使用System.Web.Script.Serialization;
命名空间测试
{
班级计划
{
公共静态void Main()
{
{
string shop_json=new WebClient()。下载字符串(“https://www.supremenewyork.com/mobile_stock.json");
JavaScriptSerializer shop_object=新的JavaScriptSerializer();
items[]shirt\u stock=shop\u对象。反序列化(shop\u json);
控制台写入线(衬衫库存[1]);
}
}
}
}
我得到一个错误:

找不到类型SUPBOTTESTING的默认构造函数。项[]


您的问题是,您正在使用一个类来加载JSON数据,您应该在其中使用一个结构,或者您也可以创建一个不带参数的构造函数,并将所有变量设置为默认值,这是一项大量的工作,因此只需将
class
替换为
struct

public struct 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; }
}
另外,请遵守C#命名约定,您应该能够做到这一点,因为大多数JSON解析器默认不区分大小写


更多信息:如果你不定义一个默认构造函数,那么一个类实际上没有一个合适的默认构造函数,而作为一个结构,它总是有一个默认构造函数,所以当JSON解析器想要初始化你的类时,它不能初始化你的类,因为默认构造函数没有定义。

你不需要指定默认构造函数。问题是,我认为您没有正确检查json数据。因为您的
items
类不在json的第一级。您需要创建几个类,以便更准确地进行反序列化

首先,您需要知道,这个
json
文件有很多不良气味和不良做法

请注意,在继续之前,您需要安装
Newtonsoft.Json
。将json反序列化为C#类更方便

然而,我写了一个反序列化它的正确方法:

public class BaseItem
{

    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; }
}
public class Shirt : BaseItem { }
public class Bag : BaseItem { }
public class Accessory : BaseItem { }
public class Pant : BaseItem { }
public class Jacket : BaseItem { }
public class Skate : BaseItem { }
public class Hat : BaseItem { }
public class Sweatshirt : BaseItem { }
public class TopsSweater : BaseItem { }
public class New : BaseItem { }
public class RootObject
{
    public List<object> Unique_image_url_prefixes { get; set; }
    public ProductsAndCategories Products_and_categories { get; set; }
    public string Release_date { get; set; }
    public string Release_week { get; set; }
}
public class ProductsAndCategories
{
    public List<Shirt> Shirts { get; set; }
    public List<Bag> Bags { get; set; }
    public List<Accessory> Accessories { get; set; }
    public List<Pant> Pants { get; set; }
    public List<Jacket> Jackets { get; set; }
    public List<Skate> Skate { get; set; }
    public List<Hat> Hats { get; set; }
    public List<Sweatshirt> Sweatshirts { get; set; }
    [JsonProperty("Tops/Sweaters")]
    public List<TopsSweater> TopsSweaters { get; set; }
    public List<New> New { get; set; }
}

好的,这是解决办法。您的想法是正确的,但需要了解Json数据的结构

您正在将
反序列化
对象的数组中
,而返回的Json数据本身不是数组或列表。它包含作为数组的子节点,因此您需要相应地构造
对象
,以获得成功的数据分解

这里我使用了
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);
     }
}
你知道我在这里做了什么吗

因此,您的Json数据包含一个父节点
products\u和\u categories
,其子节点包含一个
Shirts
数组,这就是您想要的

StockObject
类包含类型为object
ProductsCats
的名为
Products\u和\u categories
Parent
属性

ProductsCats
对象包含类型为
Details
的属性
Shirts
,该属性是一个数组,将在
反序列化过程中使用

希望这有帮助?

静态void Main(字符串[]args)
 static void Main(string[] args)
    {
        string shop_json = new WebClient().DownloadString("https://www.supremenewyork.com/mobile_stock.json");

        JavaScriptSerializer shop_object = new JavaScriptSerializer();
        var shirt_stock = shop_object.Deserialize<NewYarkItems>(shop_json);

        var v = shirt_stock;

    }


public class NewYarkItems
{
    public dynamic unique_image_url_prefixes { get; set; }
    public products_and_categories products_And_Categories { get; set; }
    public string release_date { get; set; }
    public string release_week { get; set; }
}

public class products_and_categories
{
    public List<items> Jackets { get; set; }
}


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; }
}
{ string shop_json=new WebClient()。下载字符串(“https://www.supremenewyork.com/mobile_stock.json"); JavaScriptSerializer shop_object=新的JavaScriptSerializer(); var shirt\u stock=shop\u对象。反序列化(shop\u json); var v=衬衫库存; } 公共类NewYarkItems { 公共动态唯一\u图像\u url\u前缀{get;set;} 公共产品和类别产品和类别{get;set;} 公共字符串发布日期{get;set;} 公共字符串发布_week{get;set;} } 公共类产品\u和\u类别 { 公共列表{get;set;} } 公共类项目 { 公共字符串名称{get;set;} 公共int id{get;set;} 公共字符串图像\u url{get;set;} 公共字符串图像\u url\u hi{get;set;} 公共整数价格{get;set;} 公共内部销售价格{get;set;} 公共bool new_项{get;set;} 公共int位置{get;set;} 公共字符串类别\u名称{get;set;} 公共整数价格_欧元{get;set;} 公开国际销售价格欧元{get;set;} }
为什么要将它们全部合并?那是行不通的。再次生成类并使用它。嘿,我理解你的想法和你的解决方案的tyvm。不过,当我尝试运行这段代码时,我会得到输出“SUPBOTTY.StockObject”。@AhmedSoliman我已经编辑了我的答案,请现在查看,并让我知道它是否对您有帮助?如果您满意,请将其标记为答案?请注意,在修正中,我仅显示了
衬衫
,以向您展示如何显示值。类似情况也适用于其他人,但您需要考虑如何有效显示数据的策略。:)感谢您的帮助,我尝试了代码,但我收到一个错误,即“shirtBuilder.AppendLine($”Name:{s.Name}”);“抱歉,刚刚修复了:)行中的当前上下文中不存在's'
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; }
}
 static void Main(string[] args)
    {
        string shop_json = new WebClient().DownloadString("https://www.supremenewyork.com/mobile_stock.json");

        JavaScriptSerializer shop_object = new JavaScriptSerializer();
        var shirt_stock = shop_object.Deserialize<NewYarkItems>(shop_json);

        var v = shirt_stock;

    }


public class NewYarkItems
{
    public dynamic unique_image_url_prefixes { get; set; }
    public products_and_categories products_And_Categories { get; set; }
    public string release_date { get; set; }
    public string release_week { get; set; }
}

public class products_and_categories
{
    public List<items> Jackets { get; set; }
}


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