Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.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# JSON.net-将嵌套对象反序列化为单个列表_C#_Json_Json.net - Fatal编程技术网

C# JSON.net-将嵌套对象反序列化为单个列表

C# JSON.net-将嵌套对象反序列化为单个列表,c#,json,json.net,C#,Json,Json.net,我正在尝试使用JSON.net对C#对象的JSON响应进行反序列化。 我在将嵌套对象转换为C#列表时遇到问题。 我得到下面的JSON响应 我想将所有节点放在C#的一个列表中variants下 我如何编辑我的反序列化程序,使其能够正确反序列化 { "feature_id": "48808", "feature_code": "", "company_id": "1",

我正在尝试使用JSON.net对C#对象的JSON响应进行反序列化。
我在将嵌套对象转换为C#列表时遇到问题。
我得到下面的JSON响应
我想将所有节点放在C#的一个
列表中
variants

我如何编辑我的反序列化程序,使其能够正确反序列化

{
  "feature_id": "48808",
  "feature_code": "",
  "company_id": "1",
  "feature_type": "S",
  "parent_id": "0",
  "display_on_product": "Y",
  "display_on_catalog": "N",
  "display_on_header": "N",
  "description": "",
  "lang_code": "nl",
  "prefix": "",
  "suffix": "",
  "categories_path": "30,149,161,218,219,220,221,222,483",
  "full_description": "",
  "status": "A",
  "comparison": "Y",
  "position": "8830",
  "purpose": "find_products",
  "feature_style": "text",
  "filter_style": "checkbox",
  "variants": {
    "61719": {
      "variant_id": "61719",
      "variant": "CEE 230V\\/16A",
      "description": "CEE 230V\\/16A",
      "page_title": "",
      "meta_keywords": "",
      "meta_description": "",
      "lang_code": "nl",
      "feature_id": "48808",
      "url": "",
      "color": "",
      "position": "0",
      "seo_name": null,
      "seo_path": null,
      "image_pair": null
    },
    "61720": {
      "variant_id": "61720",
      "variant": "CEE 400V\\/16A",
      "description": "CEE 400V\\/16A",
      "page_title": "",
      "meta_keywords": "",
      "meta_description": "",
      "lang_code": "nl",
      "feature_id": "48808",
      "url": "",
      "color": "",
      "position": "0",
      "seo_name": null,
      "seo_path": null,
      "image_pair": null
    },
    "61721": {
      "variant_id": "61721",
      "variant": "CEE 400V\\/32A",
      "description": "CEE 400V\\/32A",
      "page_title": "",
      "meta_keywords": "",
      "meta_description": "",
      "lang_code": "nl",
      "feature_id": "48808",
      "url": "",
      "color": "",
      "position": "0",
      "seo_name": null,
      "seo_path": null,
      "image_pair": null
    },
    "61722": {
      "variant_id": "61722",
      "variant": "CEE 400V\\/63A",
      "description": "CEE 400V\\/63A",
      "page_title": "",
      "meta_keywords": "",
      "meta_description": "",
      "lang_code": "nl",
      "feature_id": "48808",
      "url": "",
      "color": "",
      "position": "0",
      "seo_name": null,
      "seo_path": null,
      "image_pair": null
    }
  }
}
C级:

使用System.Collections.Generic;
利用制度全球化;
使用Newtonsoft.Json;
使用Newtonsoft.Json.Converters;
使用Newtonsoft.Json.Serialization;
命名空间CsFeature
{
公共部分类特征
{
[JsonProperty(“feature_id”)]公共字符串FeatureId{get;set;}
[JsonProperty(“feature_code”)]公共字符串FeatureCode{get;set;}
[JsonProperty(“company_id”)]公共字符串CompanyId{get;set;}
[JsonProperty(“feature_type”)]公共字符串FeatureType{get;set;}
[JsonProperty(“parent_id”)]公共字符串ParentId{get;set;}
[JsonProperty(“产品上的显示”)]公共字符串DisplayOnProduct{get;set;}
[JsonProperty(“在目录上显示”)公共字符串DisplayOnCatalog{get;set;}
[JsonProperty(“在标题上显示标题”)]公共字符串DisplayOnHeader{get;set;}
[JsonProperty(“description”)]公共字符串描述{get;set;}
[JsonProperty(“语言代码”)]公共字符串语言代码{get;set;}
[JsonProperty(“prefix”)]公共字符串前缀{get;set;}
[JsonProperty(“后缀”)]公共字符串后缀{get;set;}
[JsonProperty(“categories_path”)]公共字符串CategoriesPath{get;set;}
[JsonProperty(“full_description”)]公共字符串FullDescription{get;set;}
[JsonProperty(“status”)]公共字符串状态{get;set;}
[JsonProperty(“比较”)]公共字符串比较{get;set;}
[JsonProperty(“position”)]公共字符串位置{get;set;}
[JsonProperty(“变量”)]公共列表变量{get;set;}
}
公共类变体
{
[JsonProperty(“variant_id”)]公共字符串VariantId{get;set;}
[JsonProperty(“variant”)]公共字符串VariantVariant{get;set;}
[JsonProperty(“description”)]公共字符串描述{get;set;}
[JsonProperty(“页面标题”)]公共字符串页面标题{get;set;}
[JsonProperty(“meta_关键字”)]公共字符串MetaKeywords{get;set;}
[JsonProperty(“meta_description”)]公共字符串MetaDescription{get;set;}
[JsonProperty(“语言代码”)]公共字符串语言代码{get;set;}
[JsonProperty(“feature_id”)]公共字符串FeatureId{get;set;}
[JsonProperty(“url”)]公共字符串url{get;set;}
[JsonProperty(“color”)]公共字符串颜色{get;set;}
[JsonProperty(“position”)]公共字符串位置{get;set;}
[JsonProperty(“seo_name”)]公共对象SeoName{get;set;}
[JsonProperty(“seo_path”)]公共对象SeoPath{get;set;}
[JsonProperty(“图像对”)]公共对象图像对{get;set;}
}
公共部分类特征
{
来自json的公共静态特性(字符串json)
{
返回JsonConvert.DeserializeObject(json,Converter.Settings);
}
public static void HandleDeserializationError(对象发送方,ErrorEventArgs errorArgs)
{
errorArgs.ErrorContext.Handled=true;
}
}
公共静态类序列化
{
公共静态字符串ToJson(此功能本身)
{
返回JsonConvert.SerializeObject(self,Converter.Settings);
}
}
公共静态类转换器
{
公共静态只读JsonSerializerSettings设置=新JsonSerializerSettings
{
MetadataPropertyHandling=MetadataPropertyHandling.Ignore,
DateParseHandling=DateParseHandling.None,
NullValueHandling=NullValueHandling.Ignore,
DefaultValueHandling=DefaultValueHandling.Ignore,
转换器=
{
新的IsoDateTimeConverter{DateTimeStyles=DateTimeStyles.AssumeUniversal}
},
Error=功能。HandleDeserializationError
};
}
}

您可以采取的一种方法是
.Parse()
将JSON解析为
动态对象

例如:

string jsonString = @"Your JSON String";

dynamic parsedJson = JValue.Parse(jsonString);
之后,您应该能够使用LINQ遍历
动态
对象并提取
变量

资料来源:


    • 以下代码将解决您的问题:

      • 创建将保留
        列表的主类
      • 创建定义
        变体项目类型的类
      然后使用以下命令反序列化:

      JsonList jsonList = new JsonList(); 
      jsonList = JsonConvert.DeserializeObject<JsonList>(YourJSONContentString);
      
      
      JsonList JsonList=new JsonList();
      jsonList=JsonConvert.DeserializeObject(您的JSONContentString);
      
      然后在
      jsonList
      对象中有所有JSON响应项,您可以根据需要使用它们。

      与您的代码相关的变量名称必须与您在我创建的类中看到的相同。

      如果将变体模型定义为
      IList
      ?@EamonnMcEvoy,则可能会起作用。不幸的是,在我的情况下,它不会起作用,因为序列化时我需要它是一个列表,不是多个对象。我回答了你的问题,请将我的答案标记为你问题的答案,因为它解决了你的问题。谢谢,我想你需要反序列化变体,分两步得到变体列表。首先转换为键值对集合,然后将变量值转换为列表。使用
      字典
      作为
      变量
      属性。这将允许您处理数字键。请参阅链接的重复问题。请参阅我更新的帖子和我现在使用的C#类,这与您尝试执行的操作相同,反序列化后会导致变体列表为空。@Luminai,发生这种情况是因为变体(标题)是数字……是的,我知道,这就是我试图解决的问题。最好使用自定义JsonConvertor或somet
      public class JsonList
      {
          public List<MainListKeep> ListToKeep { get; set; }
      }
      
      public class MainListKeep
      {
         public string feature_id { get; set; }
         public string feature_code { get; set; }
         public string company_id{ get; set; }
         public string feature_type { get; set; }
         public string parent_id{ get; set; }
         ...
         ...
         ...
         ...
      
         public List<VariantsList> variants{ get; set; }
         
      }
      
      
      public class VariantsList 
      {
         public string variant_id { get; set; }
         public string variant { get; set; }
         public string description { get; set; }
         public string page_title { get; set; }
         ...
         ...
         ...
         ...
      }
          
      
      JsonList jsonList = new JsonList(); 
      jsonList = JsonConvert.DeserializeObject<JsonList>(YourJSONContentString);