skip to Main Content

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


  1. I assume you have a class like this

     class GradeSheet
     {
         public string? StudentName { get; set; } = string.Empty;
         public int StudentRollNo { get; set; }
         public int StudentClass { get; set;}
         public double MaekringSubjectMarks { get; set; }
         public double StatSubjectMarks { get; set; }
         public double MisSubjectMarks { get; set; }
         public double StarategySubjectMarks { get; set; }
         public double BRMSubjectMarks { get; set; }
     }
    

    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?

    Login or Signup to reply.
  2. Your Problem Ibram Reda already pointed out was simply that you never called a function to write anything to a 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, Formatting.Indented);
    
        Console.WriteLine(gradeJson);
        
        string path = "C:\YourPath\FileName.json"
        
        File.WriteAllText(path, gradeJson);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search