c# json string code example
Example 1: object to json c#
using Newtonsoft.Json;
var jsonString = JsonConvert.SerializeObject(obj);
Example 2: create json string c#
var my_jsondata = new
{
Host = @"sftp.myhost.gr",
UserName = "my_username",
Password = "my_password",
SourceDir = "/export/zip/mypath/",
FileName = "my_file.zip"
};
string json_data = JsonConvert.SerializeObject(my_jsondata);
Console.WriteLine(json_data);
JObject json_object = JObject.Parse(json_data);
Console.WriteLine((string)json_object["Host"]);
Console.WriteLine((string)json_object["UserName"]);
Console.WriteLine((string)json_object["Password"]);
Console.WriteLine((string)json_object["SourceDir"]);
Console.WriteLine((string)json_object["FileName"]);
Example 3: json property C#
public class Videogame
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("release_date")]
public DateTime ReleaseDate { get; set; }
}
Example 4: string to json c#
JObject json = JObject.Parse(str);