如何通过json.net序列化单个值?

时间:2013-05-14 12:24:26

标签: c# json serialization

我正在使用Student序列化json.net类。

我想在序列化对象中添加一个属性(称为cheak,该属性不是Student类的成员。

这是班级学生:

public class Student 
    {
        public int ID { get; set; }
        [JsonIgnore]
        public Faculty Faculty { get; set; }
        [JsonProperty("Faculty")]
        public string FacultyName { get { return Faculty.name; } }
        public double AVG { get; set; }
        public DateTime DateOfBirth { get; set; }
        public string EducationInfo { get; set; }
        public string FatherName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string MotherName { get; set; }
        public string Password { get; set; }
        public string PersonalInfo { get; set; }
    }

json:

{
  "ID": 24,
  "Faculty": "engen ",
  "AVG": 3.0,
  "DateOfBirth": "1990-02-02T00:00:00",
  "EducationInfo": "GOOD",
  "FatherName": "EEWF",
  "FirstName": "FFEWR",
  "LastName": "ERF",
  "MotherName": "ERF",
  "Password": null,
  "PersonalInfo": "ERF",
}

我想将属性“cheak”添加到序列化对象:

{
  **cheak:true
  "ID": 24,
  "Faculty": "engen ",
  "AVG": 3.0,
  "DateOfBirth": "1990-02-02T00:00:00",
  "EducationInfo": "GOOD",
  "FatherName": "EEWF",
  "FirstName": "FFEWR",
  "LastName": "ERF",
  "MotherName": "ERF",
  "Password": null,
  "PersonalInfo": "ERF",
}

1 个答案:

答案 0 :(得分:0)

如果你需要能够反序列化以及序列化对象,我会将学生继承到像这样的新类

public class StudentJson : Student 
{
    public bool cheak { get; set; }
}

然后您可以将其传递给序列化。