问题描述
我在这里找到了同样的问题...
I found the same question here...
反序列化嵌套JSON Json.NET使用注释将结构构造为扁平化的类
...但是没有正确的答案. 最好的建议之一是将嵌套对象包装到新类中,但是这种方法引入了另一个问题:乐高名称.在我的示例中,此类的最逻辑名称是与父类相同的名称,当然是不可能的.我的示例很简单,我只想消除父类中的语言"属性.有人可以帮我做吗?
...but without a proper answer. One of the best suggestion is to wrap the nested object in a new class but this approach introduces another issue: lego name. In my example the most logic name for this class is the same name that parent class and of course is not possible. My example is simple, I just want to eliminate the "language" property in the parent class. Can somebody help me to do it?
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);
json的示例
{
"languages": [
{
"id": 1,
"language": { "name": "Spanish" },
"proficiency": {
"level": "native_or_bilingual",
"name": "Native or bilingual proficiency"
}
},
{
"id": 2,
"language": { "name": "English" },
"proficiency": {
"level": "full_professional",
"name": "Full professional proficiency"
}
},
{
"id": 3,
"language": { "name": "Japanese" },
"proficiency": {
"level": "elementary",
"name": "Elementary proficiency"
}
}
]
}
推荐答案
在JSON属性名称与c#命名约定冲突的情况下,可以在序列化过程中使用DataMember
或JsonProperty
批注替换其他名称.
In cases where JSON property names conflict with c# naming conventions, you can use DataMember
or JsonProperty
annotations to substitute a different name during serialization.
例如,以下内容可用于DataContractJsonSerializer
和Json.NET:
For instance, the following works with both DataContractJsonSerializer
and 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特定的属性,那么以下内容是等效的:
If you find the opt-in nature of DataContract
attributes to be a nuisance and prefer to use Json.NET-specific attributes, then the following is equivalent:
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);
}
}
}
这篇关于将JSON反序列化为扁平化的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!