how to create json file in c#
//Create my object
var my_jsondata = new
{
Host = @"sftp.myhost.gr",
UserName = "my_username",
Password = "my_password",
SourceDir = "/export/zip/mypath/",
FileName = "my_file.zip"
};
//Tranform it to Json object
string json_data = JsonConvert.SerializeObject(my_jsondata);
//Print the Json object
Console.WriteLine(json_data);
//Parse the json object
JObject json_object = JObject.Parse(json_data);
//Print the parsed Json object
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"]);
//open file stream
using (StreamWriter file = File.CreateText(@"D:\path.txt"))
{
JsonSerializer serializer = new JsonSerializer();
//serialize object directly into file stream
serializer.Serialize(file, _data);
}//For any of the JSON parse, use the website
//http://json2csharp.com/
//(easiest way) to convert your JSON into
//C# class to deserialize your JSON into C# object.
public class FormatClass
{
public string JsonName { get; set; }
public string JsonPass { get; set; }
}
//Then use the JavaScriptSerializer (from System.Web.Script.Serialization),
// in case you don't want any third party DLL like newtonsoft.
public void Read()
{
using (StreamReader Filejosn = new StreamReader("Path.json"))
{
JavaScriptSerializer jss = new JavaScriptSerializer();
var Items = jss.Deserialize<FormatClass>(Filejosn.ReadToEnd());
// Add Code here :)
}
}
//by ahmed ashraf +201111490105