Here from the following code, I want to generate JSON file using Newtonsoft.Json.Linq
Serialization. But in my case It just runs the program on console and could not generate .JSON
file.
public static void Main(string[] args)
{
GradeSheet gradeSheet = new GradeSheet();
Console.WriteLine("Student Name: ");
gradeSheet.StudentName = Console.ReadLine();
Console.WriteLine("Roll Number: ");
gradeSheet.StudentRollNo = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Class (present): ");
gradeSheet.StudentClass = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("nnEnter marks n");
Console.WriteLine("Marketing: ");
gradeSheet.MaekringSubjectMarks = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Statistics: ");
gradeSheet.StatSubjectMarks = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("MIS: ");
gradeSheet.MisSubjectMarks = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Strategy: ");
gradeSheet.StarategySubjectMarks = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("BRM: ");
gradeSheet.BRMSubjectMarks = Convert.ToDouble(Console.ReadLine());
string gradeJson = JsonConvert.SerializeObject(gradeSheet);
Console.WriteLine(gradeJson);
}
It should generate .JSON
File
2
Answers
I assume you have a class like this
I Added "using Newtonsoft.Json;" and the tested the code. My output is this json
{"StudentName":"behzad","StudentRollNo":1,"StudentClass":2,"MaekringSubjectMarks":1.0,"StatSubjectMarks":1.0,"MisSubjectMarks":1.0,"StarategySubjectMarks":1.0,"BRMSubjectMarks":1.0}
Isn’t it ok? what’s the problem with this json?
Your Problem Ibram Reda already pointed out was simply that you never called a function to write anything to a file.