pariwesh07 pariwesh07 - 24 days ago 4x
C# Question

want to store multiple values in a string and Send it in a json format

I am trying to receive values in a single variable which is coming from some method and then convert all these values in Json format for sending.
Here i am talking about studentName,studentEmail,studentPhoneNumber and studentPassw which is coming from method 'getSysFileJsonSendToServer' called somewhere now inside this method i want to store all these variable to store in a single varible "concat" and change it in json format for sending .
Below is my code where i am trying

public async static Task<string> getSysFileJsonSendToServer(string studentName, string studentEmail, string studentPhoneNumber, string studentPassw)
DateTime dateTimeCurrent = await WebServiceManager.GetDateFromServer();
DateTime localTime = dateTimeCurrent.ToLocalTime();
string localDateTimeString = Utility.getFormatedDate(localTime);
string finalJsonObjectToServer = string.Empty;

Stream serverStream = null;
string readData = string.Empty;
string msg = "Conected to Chat Server ...";
string concat= {"name":"studentName","phone":"studentPhoneNumber","mail_id":"studentEmail","studentPassw":"hkfgd","profile":"God","status":"1","created_at":"localDateTimeString"}


I recommend using a JSON library to accomplish what you're trying to do - they save you a lot of time in coding, and provide consistency in serialization and deserialization. My recommendation is Newtsonsoft Json.NET. Here's a quick example for you:

class Student {
   public string Name { get; set; }
   public string Phone { get; set; }
   //more properties


var student = new Student {
    Name = "My Name",
    Phone = "555-453-6547"

string json = JsonConvert.SerializeObject(student);//now in json format

//now a Student object
Student deserializedJson = JsonConvert.DeserializeObject<Student>(json);

Newtonsoft also fully supports async if you need to go that route.

To use Newtonsoft, you can go to your nuget package manager:

Tools → Nuget Package Manager → Manage Nuget Packages for Solution..

Then search for Newtsonsoft and install. After completion, make sure you add the DLL reference in your list of references, then add a using statement:

using Newtonsoft.Json;