-3

I have a JSON File as below ,

{
  "student": {
    "fullName": "No Name",
        "id": 0001
  },
  "message": "Lets see if this is displayed!"
}

I want to write some logic in my WPF application where when someone clicks the submit button after filling in the corresoonding text fields those values will be serialized into this JSON.

In the sense for example if someone enters Dwayne as the name , 2222 as the ID and "Hello" as the message I want the JSON file to look as

{
  "student": {
    "fullName": "Dwayne",
        "id": 2222 
  },
  "message": "Hello"
}

Here is what I have implmented thus far in my code behind

I have the StudentData.cs class

 public class StudentData
    {
        public StudentData()
        {

        }

        public string Name { get; set; }

        public string ID { get; set; }

        public string Message { get; set; }

    }

and in the xaml.cs where the save is executed I get the necessary data and save it as an object

 string fullName = textName.Text;
            string ID = textId.Text;
            string message = textMessage.Text;

            StudentData stuData = new StudentData();
            stuData .Name = fullName;
            stuData .ID = ID;
            stuData .Message = message;

Can anyone help on what I can do next ?

Devss
  • 67
  • 7

2 Answers2

1

You should use a JSON serialisation package, I prefer Newtonsoft. https://www.newtonsoft.com/json

You would then serialise your data into JSON and write it like this:

List<StudentData> myStudentData = new List<StudentData>();
//Add data

string json = JsonConvert.SerializeObject(myStudentData);
File.WriteAllText("c:\\path\\to\\yourfile.json", json );

This will overwrite the file you specify with the new data.

BUT:

Your data structure is not correct to achieve the JSON you wish to output. So you either need to change your expectation of the output of the data, or would need to change your classes.

With the JSON example you have then message is only shown once for the whole output. If you want this then you need to use the following classes.

public class Student
{
    public string fullName { get; set; }
    public int id { get; set; }
}

public class RootObject
{
    public List<Student> student { get; set; }
    public string message { get; set; }
}

Then you will of course have to change your creation and output of objects like this;

RootObject root = new RootObject();
root.message = "Hello";
List<StudentData> myStudentData = new List<StudentData>();

//add data to student
root.student = myStudentData;
string json = JsonConvert.SerializeObject(root);
File.WriteAllText("c:\\path\\to\\yourfile.json", json );

If you wish for the message to be per student, then you keep your current class structure and you will get the following JSON output.

{
  "student": {
    "fullName": "Dwayne",
    "id": 2222,
    "message": "Hello" 
  }
}
jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51
0

You can use Newtonsoft.json library to convert your object to json.

var student = JsonConvert.SerializeObject(StudentData);

newtonsoft.json

Rishi Shah
  • 46
  • 6