c# – How can one feed such a value in C #?

Question:

How to feed such a value: {"reg":"date","univer":"","name":""} ?

 request.AddParam("listing" , "{"reg":"date","univer":"","name":""}");

I tried with slashes but it didn’t work, please help me solve this problem …

Answer:

Of course, I'm not against writing the Json object manually, but I am for serialization in order to avoid all sorts of errors, so something like this:

using Newtonsoft.Json;
...

public class RequestListingModel
{
    [JsonProperty("reg")]
    public DateTime Reg {get;set;}
    [JsonProperty("univer")]
    public string Univer {get;set;}
    [JsonProperty("name")]
    public string Name {get;set;}
}

var jString = JsonConvert.SerializeObject(new RequestListingModel{
    Reg = DateTime.Now,
    Univer = "МГУ",
    Name = "Петя Иванов"
});
request.AddParam("listing" , jString);
Scroll to Top