Skip to content

Instantly share code, notes, and snippets.

@estevanjantsk
Created November 26, 2015 14:40
Show Gist options
  • Save estevanjantsk/9e2203b81c33937bd524 to your computer and use it in GitHub Desktop.
Save estevanjantsk/9e2203b81c33937bd524 to your computer and use it in GitHub Desktop.
JSONNETExample e JavaScriptSerializerExample
using Newtonsoft.Json;
// deserialize JSON directly from a file
String JSONstring = File.ReadAllText("JSON.json");
Person p1 = JsonConvert.DeserializeObject<Person>(JSONstring);
Console.WriteLine(p1);
// output JSON file
Person p2 = new Person { name = "ben", age = 46 };
string outputJSON = JsonConvert.SerializeObject(p2);
File.WriteAllText("Output.json", outputJSON);
//include
using System.Web.Script.Serialization;
using System.IO;
//deserialize JSON from file
String JSONstring = File.ReadAllText("JSON.json");
JavaScriptSerializer ser = new JavaScriptSerializer();
Person p1 = ser.Deserialize<Person>(JSONstring);
Console.WriteLine(p1);
//output JSON file
Person p2 = new Person() { name = "Ben", age = 58 };
string outputJSON = ser.Serialize(p2);
File.WriteAllText("Output.json", outputJSON);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment