text stringlengths 0 13.4k |
|---|
JavaScriptSerializer JSS = new JavaScriptSerializer(); |
Dictionary<string, object> parsedObj = JSS.Deserialize<Dictionary<string, object>>(rawJSON); |
string name = parsedObj["Name"].toString(); |
ArrayList numbers = (ArrayList)parsedObj["Numbers"] |
Note: The JavaScriptSerializer object was introduced in .NET version 3.5 |
Section 32.2: Serialization using Json.NET |
[JsonObject("person")] |
public class Person |
{ |
[JsonProperty("name")] |
public string PersonName { get; set; } |
[JsonProperty("age")] |
public int PersonAge { get; set; } |
[JsonIgnore] |
public string Address { get; set; } |
} |
Person person = new Person { PersonName = "Andrius", PersonAge = 99, Address = "Some address" }; |
string rawJson = JsonConvert.SerializeObject(person); |
Console.WriteLine(rawJson); // {"name":"Andrius","age":99} |
Notice how properties (and classes) can be decorated with attributes to change their appearance in resulting json |
string or to remove them from json string at all (JsonIgnore). |
More information about Json.NET serialization attributes can be found here. |
In C#, public identifiers are written in PascalCase by convention. In JSON, the convention is to use camelCase for all |
names. You can use a contract resolver to convert between the two. |
using Newtonsoft.Json; |
using Newtonsoft.Json.Serialization; |
public class Person |
{ |
public string Name { get; set; } |
public int Age { get; set; } |
[JsonIgnore] |
GoalKicker.com – .NET Framework Notes for Professionals |
107 |
public string Address { get; set; } |
} |
public void ToJson() { |
Person person = new Person { Name = "Andrius", Age = 99, Address = "Some address" }; |
var resolver = new CamelCasePropertyNamesContractResolver(); |
var settings = new JsonSerializerSettings { ContractResolver = resolver }; |
string json = JsonConvert.SerializeObject(person, settings); |
Console.WriteLine(json); // {"name":"Andrius","age":99} |
} |
Section 32.3: Serialization-Deserialization using |
Newtonsoft.Json |
Unlike the other helpers, this one uses static class helpers to serialize and deserialize, hence it is a little bit easier |
than the others to use. |
using Newtonsoft.Json; |
var rawJSON = "{\"Name\":\"Fibonacci Sequence\",\"Numbers\":[0, 1, 1, 2, 3, 5, 8, 13]}"; |
var fibo = JsonConvert.DeserializeObject<Dictionary<string, object>>(rawJSON); |
var rawJSON2 = JsonConvert.SerializeObject(fibo); |
Section 32.4: Deserialization using Json.NET |
internal class Sequence{ |
public string Name; |
public List<int> Numbers; |
} |
// ... |
string rawJSON = "{\"Name\":\"Fibonacci Sequence\",\"Numbers\":[0, 1, 1, 2, 3, 5, 8, 13]}"; |
Sequence sequence = JsonConvert.DeserializeObject<Sequence>(rawJSON); |
For more information, refer to the Json.NET official site. |
Note: Json.NET supports .NET version 2 and higher. |
Section 32.5: Dynamic binding |
Newtonsoft's Json.NET allows you to bind json dynamically (using ExpandoObject / Dynamic objects) without the |
need to create the type explicitly. |
Serialization |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.