1 //Create my object
2 var my_jsondata = new
3 {
4 Host = @"sftp.myhost.gr",
5 UserName = "my_username",
6 Password = "my_password",
7 SourceDir = "/export/zip/mypath/",
8 FileName = "my_file.zip"
9 };
10
11 //Tranform it to Json object
12 string json_data = JsonConvert.SerializeObject(my_jsondata);
13
14 //Print the Json object
15 Console.WriteLine(json_data);
16
17 //Parse the json object
18 JObject json_object = JObject.Parse(json_data);
19
20 //Print the parsed Json object
21 Console.WriteLine((string)json_object["Host"]);
22 Console.WriteLine((string)json_object["UserName"]);
23 Console.WriteLine((string)json_object["Password"]);
24 Console.WriteLine((string)json_object["SourceDir"]);
25 Console.WriteLine((string)json_object["FileName"]);
26
1//open file stream
2using (StreamWriter file = File.CreateText(@"D:\path.txt"))
3{
4 JsonSerializer serializer = new JsonSerializer();
5 //serialize object directly into file stream
6 serializer.Serialize(file, _data);
7}
1
2//For any of the JSON parse, use the website
3//http://json2csharp.com/
4//(easiest way) to convert your JSON into
5//C# class to deserialize your JSON into C# object.
6
7public class FormatClass
8{
9 public string JsonName { get; set; }
10 public string JsonPass { get; set; }
11}
12//Then use the JavaScriptSerializer (from System.Web.Script.Serialization),
13// in case you don't want any third party DLL like newtonsoft.
14public void Read()
15{
16 using (StreamReader Filejosn = new StreamReader("Path.json"))
17 {
18 JavaScriptSerializer jss = new JavaScriptSerializer();
19 var Items = jss.Deserialize<FormatClass>(Filejosn.ReadToEnd());
20 // Add Code here :)
21 }
22}
23//by ahmed ashraf +201111490105
24