将JSON反序列化为flattened类

时间:2015-05-13 18:45:31

标签: json json.net deserialization

我在这里找到了同样的问题......

Deserializing nested JSON structure to a flattened class with Json.NET using annotations

......但没有正确答案。 最好的建议之一是将嵌套对象包装在一个新类中,但这种方法引入了另一个问题:lego name。在我的示例中,此类的最大逻辑名称与父类和当然是不可能的。我的例子很简单,我只想消除父类中的“language”属性。有人可以帮我做吗?

String

json的例子:

using Newtonsoft.Json;

public partial class NamedType
{
    public string Name { get; set; }
}

public class Proficiency
{
    public string Level { get; set; }

    public string Name { get; set; }
}

public class Language
{
    public int Id { get; set; }

    public string Name { get; set; }

    //public Language Language { get; set; } //Compiler error
    //public Language Value { get; set; } //Not correct
    //public NamedType Language { get; set; } //Compiler error
    //public NamedType Value { get; set; } //Ugly, isn't?

    public Proficiency Proficiency { get; set; }
}

List<Language> languageList = JsonConvert.DeserializeObject<List<Language>>(json);

1 个答案:

答案 0 :(得分:1)

如果JSON属性名称与c#命名约定冲突,则可以使用DataMemberJsonProperty注释在序列化期间替换其他名称。

例如,以下内容适用于DataContractJsonSerializer和Json.NET:

[DataContract]
public class Language
{
    [DataContract]
    class NamedType
    {
        [DataMember]
        public string name { get; set; }
    }

    [DataContract]
    class ProficiencyType
    {
        [DataMember]
        public string level { get; set; }
        [DataMember]
        public string name { get; set; }
    }

    [DataMember(Name="id")]
    public int Id { get; set; }

    [IgnoreDataMember] // Do not serialize this property
    public string Name { get; set; }

    [IgnoreDataMember]
    public string ProficiencyLevel { get; set; }

    [IgnoreDataMember]
    public string ProficiencyName { get; set; }

    [DataMember(Name="language")] // serialize this nested class property with name "language"
    [JsonProperty(ObjectCreationHandling=ObjectCreationHandling.Replace)] // When deserializing, always create a fresh instance instead of reusing the proxy class.
    NamedType LanguageName
    {
        get
        {
            return new NamedType { name = Name };
        }
        set
        {
            Name = (value == null ? null : value.name);
        }
    }

    [DataMember(Name = "proficiency")]
    [JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace)]
    ProficiencyType Proficiency
    {
        get
        {
            return new ProficiencyType { level = ProficiencyLevel, name = ProficiencyName };
        }
        set
        {
            ProficiencyLevel = (value == null ? null : value.level);
            ProficiencyName = (value == null ? null : value.name);
        }
    }
}

如果您发现DataContract属性的选择性质是令人讨厌并且更喜欢使用特定于Json.NET的属性,那么以下内容是等效的:

public class Language
{
    class NamedType
    {
        public string name { get; set; }
    }

    class ProficiencyType
    {
        public string level { get; set; }
        public string name { get; set; }
    }

    [JsonProperty(PropertyName = "id")]
    public int Id { get; set; }

    [JsonIgnore]
    public string Name { get; set; }

    [JsonIgnore]
    public string ProficiencyLevel { get; set; }

    [JsonIgnore]
    public string ProficiencyName { get; set; }

    [JsonProperty(PropertyName = "language", ObjectCreationHandling = ObjectCreationHandling.Replace)]
    NamedType LanguageName
    {
        get
        {
            return new NamedType { name = Name };
        }
        set
        {
            Name = (value == null ? null : value.name);
        }
    }

    [JsonProperty(PropertyName = "proficiency", ObjectCreationHandling = ObjectCreationHandling.Replace)]
    ProficiencyType Proficiency
    {
        get
        {
            return new ProficiencyType { level = ProficiencyLevel, name = ProficiencyName };
        }
        set
        {
            ProficiencyLevel = (value == null ? null : value.level);
            ProficiencyName = (value == null ? null : value.name);
        }
    }
}