首页 > 解决方案 > JSON.net - 将嵌套对象反序列化为单个列表

问题描述

我正在尝试使用 JSON.net 反序列化对 C# 对象的 JSON 响应。
我在将嵌套对象转换为 C# 列表时遇到问题。
我得到下面的 JSON 响应。
我想将所有节点放在variants一个List<Variant>C# 中。
如何编辑我的反序列化器以便正确反序列化?

{
  "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#类:

using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;

namespace CsFeature
{
public partial class Feature
{
    [JsonProperty("feature_id")] public string FeatureId { get; set; }

    [JsonProperty("feature_code")] public string FeatureCode { get; set; }

    [JsonProperty("company_id")] public string CompanyId { get; set; }

    [JsonProperty("feature_type")] public string FeatureType { get; set; }

    [JsonProperty("parent_id")] public string ParentId { get; set; }

    [JsonProperty("display_on_product")] public string DisplayOnProduct { get; set; }

    [JsonProperty("display_on_catalog")] public string DisplayOnCatalog { get; set; }

    [JsonProperty("display_on_header")] public string DisplayOnHeader { get; set; }

    [JsonProperty("description")] public string Description { get; set; }

    [JsonProperty("lang_code")] public string LangCode { get; set; }

    [JsonProperty("prefix")] public string Prefix { get; set; }

    [JsonProperty("suffix")] public string Suffix { get; set; }

    [JsonProperty("categories_path")] public string CategoriesPath { get; set; }

    [JsonProperty("full_description")] public string FullDescription { get; set; }

    [JsonProperty("status")] public string Status { get; set; }

    [JsonProperty("comparison")] public string Comparison { get; set; }

    [JsonProperty("position")] public string Position { get; set; }

    [JsonProperty("variants")] public List<Variant> Variants { get; set; }
}

public class Variant
{
    [JsonProperty("variant_id")] public string VariantId { get; set; }
    [JsonProperty("variant")] public string VariantVariant { get; set; }
    [JsonProperty("description")] public string Description { get; set; }

    [JsonProperty("page_title")] public string PageTitle { get; set; }

    [JsonProperty("meta_keywords")] public string MetaKeywords { get; set; }

    [JsonProperty("meta_description")] public string MetaDescription { get; set; }

    [JsonProperty("lang_code")] public string LangCode { get; set; }

    [JsonProperty("feature_id")] public string FeatureId { get; set; }

    [JsonProperty("url")] public string Url { get; set; }

    [JsonProperty("color")] public string Color { get; set; }

    [JsonProperty("position")] public string Position { get; set; }

    [JsonProperty("seo_name")] public object SeoName { get; set; }

    [JsonProperty("seo_path")] public object SeoPath { get; set; }

    [JsonProperty("image_pair")] public object ImagePair { get; set; }
}

public partial class Feature
{
    public static Feature FromJson(string json)
    {
        return JsonConvert.DeserializeObject<Feature>(json, Converter.Settings);
    }

    public static void HandleDeserializationError(object sender, ErrorEventArgs errorArgs)
    {
        errorArgs.ErrorContext.Handled = true;
    }
}

public static class Serialize
{
    public static string ToJson(this Feature self)
    {
        return JsonConvert.SerializeObject(self, Converter.Settings);
    }
}

public static class Converter
{
    public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
    {
        MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
        DateParseHandling = DateParseHandling.None,
        NullValueHandling = NullValueHandling.Ignore,
        DefaultValueHandling = DefaultValueHandling.Ignore,
        Converters =
        {
            new IsoDateTimeConverter {DateTimeStyles = DateTimeStyles.AssumeUniversal}
        },
        Error = Feature.HandleDeserializationError
    };
}
}

标签: c#jsonjson.net

解决方案


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

  • 创建将保留您的主类List
public class JsonList
{
    public List<MainListKeep> ListToKeep { get; set; }
}
  • 创建定义类项目类型的JsonList类:
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; }
   
}
  • 创建定义项目类型的类variants

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

然后您在对象中拥有所有 JSON 响应项,jsonList您可以根据需要使用它们。
与您的代码相关的变量名称必须与您在上面我创建的类中看到的相同。


推荐阅读