DB Advanced JSON Processing
DB Advanced JSON Processing
SoftUni Team
Technical Trainers
Software University
http://softuni.bg
Table of Contents
2
Have a Question?
sli.do
#CSharpDB
3
{JSON}
{
"Id":0,
"Name":"Oil Pump",
"Description":null,
"Cost":25
}
Newtonsoft
Json.NET
JSON.NET
Better JSON Parsing for .NET Developers
What is JSON.NET?
JSON.NET is a JSON framework for .NET
More functionality than built-in functionality
Supports LINQ-to-JSON
Out-of-the-box support for parsing between JSON and XML
Open-source project: http://www.newtonsoft.com
Json.NET vs .NET Serializers: https://www.newtonsoft.com/jso
n/help/html/JsonNetVsDotNetSerializers.htm
Installing JSON.NET
To install JSON.NET use the NuGet Package Manager:
To Deserialize an object:
var objProduct =
JsonConvert.DeserializeObject<Product>(jsonProduct);
JSON.NET Features
JSON.NET can be configured to:
Indent the output JSON string
To convert JSON to anonymous types
To control the casing and properties to parse
To skip errors
JSON.NET also supports:
LINQ-to-JSON
Direct parsing between XML and JSON
Configuring JSON.NET
By default, the result is a single line of text
To indent the output string use Formatting.Indented
JsonConvert.SerializeObject(products, Formatting.Indented);
{
"pump": {
"Id": 0,
"Name": "Oil Pump",
"Description": null,
"Cost": 25.0
},
"filter": {
"Id": 0,
"Name": "Oil Filter",
"Description": null,
"Cost": 15.0
}
}
Configuring JSON.NET
Deserializing to anonymous types:
Incoming JSON
var json = @"{ 'firstName': 'Vladimir',
'lastName': 'Georgiev',
'jobTitle': 'Technical Trainer' }";
var template = new
{
FirstName = string.Empty,
LastName = string.Empty, Template
Occupation = string.Empty
};
objects
var person = JsonConvert.DeserializeAnonymousType(json, template);
JSON.NET Parsing of Objects
By default JSON.NET takes each property / field from the class
and parses it
This can be controlled using attributes:
public class User Parse Username
{ to user
[JsonProperty("user")]
public string Username { get; set; }
[JsonIgnore] Skip the property
public string Password { get; set; }
}
JSON.NET Parsing of Objects
By default JSON.NET takes each property / field from the class
and parses it
This can be controlled using ContractResolver:
DefaultContractResolver contractResolver = new DefaultContractResolver()
{
NamingStrategy = new SnakeCaseNamingStrategy()
};
Using JObject:
foreach (JToken person in people)
{
Console.WriteLine(person["FirstName"]); // Ivan
Console.WriteLine(person["LastName"]); // Petrov
}
25
LINQ-to-JSON (2)
JObjects can be queried with LINQ
var json = JObject.Parse(@"{'products': [
{'name': 'Fruits', 'products': ['apple', 'banana']},
{'name': 'Vegetables', 'products': ['cucumber']}]}");
JSON
… is cross platform data
format
…
DataContractJsonSerializer
… is the
default JSON Parser in C#
JSON.NET is a fast framework for
working with JSON data
29
https://softuni.bg/courses/databases-advanced-entity-framework
SoftUni Diamond Partners
SoftUni Organizational Partners
Trainings @ Software University (SoftUni)
Software University – High-Quality Education and
Employment Opportunities
softuni.bg
Software University Foundation
http://softuni.foundation/
Software University @ Facebook
facebook.com/SoftwareUniversity
Software University Forums
forum.softuni.bg
License
This course (slides, examples, demos, videos, homework, etc.)
is licensed under the "Creative Commons Attribution-NonCom
mercial-ShareAlike 4.0 International" license
34