Processing JSON in NET JSON JSON NET LINQtoJSON

  • Slides: 32
Download presentation
Processing JSON in. NET JSON, JSON. NET LINQ-to-JSON and JSON to XML Soft. Uni

Processing JSON in. NET JSON, JSON. NET LINQ-to-JSON and JSON to XML Soft. Uni Team Technical Trainers Software University http: //softuni. bg

Table of Contents 1. The JSON data format § Rules and Features § Usage

Table of Contents 1. The JSON data format § Rules and Features § Usage 2. JSON. NET Overview § Installation and Usage § LINQ-to-JSON § JSON to XML and XML to JSON 2

The JSON Data Format What is JSON?

The JSON Data Format What is JSON?

The JSON Data Format § JSON (Java. Script Object Notation) is a lightweight data

The JSON Data Format § JSON (Java. Script Object Notation) is a lightweight data format § Human and machine-readable § Based on the way to create objects in Java. Script § Independent of development platforms and languages § JSON data consists of: § Values (strings, numbers, etc. ) § Key-value pairs: { key : value } § Arrays: [value 1, value 2, …}

JSON Data Format § The JSON data format follows the rules of object creation

JSON Data Format § The JSON data format follows the rules of object creation in JS § Strings, numbers and Booleans are valid JSON: "this is string and is valid JSON" 3. 14 § Arrays are valid JSON: [5, 'string', true] § Objects are valid JSON (key-value pairs): { } "first. Name": "Vladimir", "last. Name": "Georgiev", "job. Title": "Technical Trainer", age: 25 true

Processing JSON in. NET How to Parse JSON in C# and. NET Framework?

Processing JSON in. NET How to Parse JSON in C# and. NET Framework?

Built-in JSON Serializers §. NET has built-in JSON serializer: § The Java. Script. Serializer

Built-in JSON Serializers §. NET has built-in JSON serializer: § The Java. Script. Serializer class § Contained in System. Web. Extensions assembly § Java. Script. Serializer can parse from object to JSON string and vice versa: var place = new Place(…); var serializer = new Java. Script. Serializer(); var json. Place = serializer. Serialize(place); var obj. Place = serializer. Deserialize<Place>(json. Place);

Java. Script Serializer Live Demo

Java. Script Serializer Live Demo

Java. Script. Serializer Features § The. NET Java. Script serializer is powerful: § Serialize

Java. Script. Serializer Features § The. NET Java. Script serializer is powerful: § Serialize objects to JSON and vice versa § Serialize / deserialize dictionaries correctly: var digits = new Dictionary<string, int> { { "one", 1 }, { "two", 2 }, }; var serializer = new Java. Script. Serializer(); string json = serializer. Serialize(digits); var d = serializer. Deserialize<Dictionary<string, int>>(json);

Java. Script. Serializer Features § Java. Script serializer has nice features: § Serializing objects

Java. Script. Serializer Features § Java. Script serializer has nice features: § Serializing objects to JSON and vice versa § Correct parsing of dictionaries: Is parsed to var digits = new Dictionary<string, int> { { "one", 1}, { "two", 2}, … }; { } "one": 1, "two": 2, …

Java. Script Serializer Features Live Demo

Java. Script Serializer Features Live Demo

JSON. NET Better JSON Parsing for. NET Developers

JSON. NET Better JSON Parsing for. NET Developers

JSON. NET § Java. Script. Serializer is good § But JSON. NET is better

JSON. NET § Java. Script. Serializer is good § But JSON. NET is better § JSON. NET: § Has better performance § Supports LINQ-to-JSON § Out-of-the-box support for parsing between JSON and XML § Open-source project: http: //www. newtonsoft. com

Installing JSON. NET § To install JSON. NET run in the Nu. Get Package

Installing JSON. NET § To install JSON. NET run in the Nu. Get Package Manager Console: $ Install-Package Newtonsoft. Json § Has two primary methods: § Serialize an object: var serialized. Place = Json. Convert. Serialize. Object(place); § Deserialize an object: var deserialized. Place = Json. Convert. Deserialize. Object<Place>(serialized. Place);

Serializing and Deserializing Objects Live Demo

Serializing and Deserializing Objects Live Demo

JSON. NET Features

JSON. NET Features

JSON. NET Features § JSON. NET can be configured to: § Indent the output

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 parse between XML and JSON

T E N. N O JS Configuring JSON. NET

T E N. N O JS Configuring JSON. NET

Configuring JSON. NET § To indent the output string use Formatting. Indented: Json. Convert.

Configuring JSON. NET § To indent the output string use Formatting. Indented: Json. Convert. Serialize. Object(place, Formatting. Indented); § Deserializing to anonymous types: var json = @"{ 'first. Name': 'Vladimir', 'last. Name': 'Georgiev', 'job. Title': 'Technical Trainer' }"; var template = new Should provide a { First. Name = string. Empty, template Last. Name = string. Empty, Occupation = string. Empty }; var person = Json. Convert. Deserialize. Anonymous. Type(json, template);

T E N. N O S J Configuring JSON. NET Live Demo

T E N. N O S J Configuring JSON. NET Live Demo

JSON. NET Object Parsing

JSON. NET Object Parsing

JSON. NET Parsing of Objects § By default JSON. NET takes each property /

JSON. NET Parsing of Objects § By default JSON. NET takes each property / field from the public interface of a class and parses it § This can be controlled using attributes: public class User { Parse Username to user [Json. Property("user")] public string Username { get; set; } } Skip the property [Json. Ignore] public string Password { get; set; }

JSON. NET Object Parsing Live Demo

JSON. NET Object Parsing Live Demo

LINQ-to-JSON Using LINQ to JSON with JSON. NET

LINQ-to-JSON Using LINQ to JSON with JSON. NET

LINQ-to-JSON § JSON. NET supports LINQ-to-JSON: var json. Obj = JObject. Parse(json); Console. Write.

LINQ-to-JSON § JSON. NET supports LINQ-to-JSON: var json. Obj = JObject. Parse(json); Console. Write. Line("Places in {0}: ", json. Obj["name"]); json. Obj["places"]. Select( pl => string. Format("{0}) {1} ({2})", index++, pl["name"], string. Join(", ", pl["categories"]. Select( cat => cat["name"])))). Print();

LINQ-to-JSON Live Demo

LINQ-to-JSON Live Demo

XML to JSON and JSON to XML

XML to JSON and JSON to XML

XML to JSON and JSON to XML § Conversions from JSON to XML are

XML to JSON and JSON to XML § Conversions from JSON to XML are done using two methods: § XML to JSON string json. From. Xml = Json. Convert. Serialize. XNode(doc); § JSON to XML XDocument xml. From. Json = Json. Convert. Deserialize. XNode(json);

XML to JSON and JSON to XML Live Demo

XML to JSON and JSON to XML Live Demo

Processing JSON in. NET ? s n o i t s e u Q

Processing JSON in. NET ? s n o i t s e u Q ? ? ? https: //softuni. bg/courses/database-applications

License § This course (slides, examples, demos, videos, homework, etc. ) is licensed under

License § This course (slides, examples, demos, videos, homework, etc. ) is licensed under the "Creative Commons Attribution. Non. Commercial-Share. Alike 4. 0 International" license § Attribution: this work may contain portions from § "Databases" course by Telerik Academy under CC-BY-NC-SA license 31

Free Trainings @ Software University § Software University Foundation – softuni. org § Software

Free Trainings @ Software University § Software University Foundation – softuni. org § Software University – High-Quality Education, Profession and Job for Software Developers § softuni. bg § Software University @ Facebook § facebook. com/Software. University § Software University @ You. Tube § youtube. com/Software. University § Software University Forums – forum. softuni. bg