I found the same question here...
Deserializing nested JSON structure to a flattened class with Json.NET using annotations
...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);
Example of 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"
}
}
]
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…