Handling extra members when deserializing with Json.net

前端 未结 2 1796
半阙折子戏
半阙折子戏 2021-01-05 09:59

Suppose I want to deserialize a set of Json data into a Person object.

class Person
{
    [DataMember]
    string name;
    [DataMember]
    int age;
    [Da         


        
2条回答
  •  半阙折子戏
    2021-01-05 10:44

    Yes,you can do it with JSON.NET:

    dynamic dycperson= JsonConvert.DeserializeObject(@"{
    'name':'Chris',
    'age':100,
    'birthplace':'UK',
    'height':170,
    'birthdate':'08/08/1913'}");
    Person person = new Person{
      name = dycperson.name,
      age=dycperson.age,
      height=dycperson.height,
      unused= new {birthplace = dycperson.birthplace, birthdate=dycperson.birthdate}
    };
    

提交回复
热议问题