Created
November 26, 2015 14:40
-
-
Save estevanjantsk/9e2203b81c33937bd524 to your computer and use it in GitHub Desktop.
JSONNETExample e JavaScriptSerializerExample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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