public class students
{
public int ID { get; set; }
public string fName { get; set; }
public string mName { get; set; }
public string lName { get; set; }
public string Dept { get; set; }
public string Degree { get; set; }
public _Semister[] ComplitedSemister { get; set; }
public string SemesterCode { get; set; }
public DateTime Year { get; set; }
}
public class _Semister
{
public string Name { get; set; }
public string Instructor { get; set; }
public int Credit { get; set; }
}
List<students> _data = new List<students>();
_data.Add(new students()
{
ID = 101,
fName = "Kamal",
mName = "Hasan",
lName = "Fahem",
Dept = "CSE",
Degree = "BSC",
ComplitedSemister =
{
Name ="dsg",
Instructor ="Jamal",
Credit =25
},
SemesterCode = "Summer",
Year = DateTime.Parse("2019-08-01")
});
How I Put Data in Json FIle for ComplitedSemister property ? I get an error that Name ,Instructor, Credit can't access. But Other property also accesss and work properly
CodePudding user response:
You need to create the appropriated object
List<students> _data = new List<students>();
_data.Add(new students()
{
ID = 101,
fName = "Kamal",
mName = "Hasan",
lName = "Fahem",
Dept = "CSE",
Degree = "BSC",
// Create the Array and populate with the object
ComplitedSemister = new _Semister[]{
new _Semister()
{
Name ="dsg",
Instructor ="Jamal",
Credit =25
}},
SemesterCode = "Summer",
Year = DateTime.Parse("2019-08-01")
});
CodePudding user response:
You declare ComplitedSemister as an array of _Semister:
public _Semister[] ComplitedSemister { get; set; }
then you try to assing as a single object:
ComplitedSemister =
{
Name ="dsg",
Instructor ="Jamal",
Credit =25
},
so it fails
I prefer using List when is possible
public List<_Semister> ComplitedSemister { get; set; }
then
ComplitedSemister = new List<_Semister> {{
Name ="dsg",
Instructor ="Jamal",
Credit =25
}},