Exporting and Importing Data

Slides:



Advertisements
Similar presentations
Processing JSON in.NET JSON, JSON.NET LINQ-to-JSON and JSON to XML SoftUni Team Technical Trainers Software University
Advertisements

Sets, Dictionaries SoftUni Team Technical Trainers Software University
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Strings and Text Processing
XML Processing SoftUni Team Database Applications Technical Trainers
Exporting and Importing Data
Helpers, Data Validation
Auto Mapping Objects SoftUni Team Database Applications
Static Members and Namespaces
Functional Programming
Databases basics Course Introduction SoftUni Team Databases basics
Abstract Classes, Abstract Methods, Override Methods
Sets, Hash table, Dictionaries
C# Basic Syntax, Visual Studio, Console Input / Output
Interface Segregation / Dependency Inversion
Introduction to MVC SoftUni Team Introduction to MVC
PHP MVC Frameworks Course Introduction SoftUni Team Technical Trainers
PHP Fundamentals Course Introduction SoftUni Team Technical Trainers
Reflection SoftUni Team Technical Trainers Java OOP Advanced
C# Database Fundamentals with Microsoft SQL Server
Classes, Properties, Constructors, Objects, Namespaces
Mocking tools for easier unit testing
Parsing JSON JSON.NET, LINQ-to-JSON
State Management Cookies, Sessions SoftUni Team State Management
EF Code First (Advanced)
PHP MVC Frameworks MVC Fundamentals SoftUni Team Technical Trainers
C# Databases Advanced with Microsoft SQL Server
Processing Sequences of Elements
EF Relations Object Composition
Entity Framework: Code First
Parsing XML XDocument and LINQ
Repeating Code Multiple Times
Inheritance Class Hierarchies SoftUni Team Technical Trainers C# OOP
Data Definition and Data Types
Databases advanced Course Introduction SoftUni Team Databases advanced
Install and configure theme
Balancing Binary Search Trees, Rotations
Entity Framework: Relations
Fast String Manipulation
Array and List Algorithms
Functional Programming
ASP.NET Razor Engine SoftUni Team ASP.NET MVC Introduction
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Exporting and Importing Data
Databases Advanced Course Introduction SoftUni Team Databases Advanced
Combining Data Structures
Best practices and architecture
Arrays and Multidimensional Arrays
Built-in Functions. Usage of Wildcards
Data Definition and Data Types
Multidimensional Arrays, Sets, Dictionaries
Extending functionality using Collections
Exporting and Importing Data
Making big SPA applications
Manual Mapping and AutoMapper Library
JSON Crash Course Traversy Media.
Functional Programming
ASP.NET Razor Engine SoftUni Team ASP.NET MVC Introduction
Introduction to TypeScript & Angular
CSS Transitions and Animations
Iterators and Comparators
Hibernate (JPA) Code First Entity Relations
Spring Data Advanced Querying
Software Quality Assurance
Polymorphism, Interfaces, Abstract Classes
Text Processing and Regex API
/^Hel{2}o\s*World\n$/
CSS Transitions and Animations
Iterators and Generators
Multidimensional Arrays
Presentation transcript:

Exporting and Importing Data JSON Exporting and Importing Data JSON SoftUni Team Technical Trainers Software University http://softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Table of Contents JSON Export JSON Import * (c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Questions sli.do #db-advanced

JSON

JSON Specifics JSON = JavaScript Object Notation JSON is a subset of JavaScript syntax JSON is a lightweight format that is used for data interchanging JSON is easy to read and write JSON is language independent JSON format is primarily used to transmit data between a server and web application The filename extension is .json

JSON Example person.json js object Key Value Variable Value Property { "firstName": "Daniel", "lastName": "Sempre", "age": 24, "isMarried": true } js object Variable Value var person = { firstName: "Daniel", lastName: "Sempre", age: 24, isMarried: true }; Property

JSON Function Client Server person.js person.json car.js car.json JavaScript Java, PHP, C# Client Server JSON PersonController.java person.js person.json CarController.java car.js car.json

JSON Structure Data is represented in name/value pairs. Curly braces hold objects Square brackets hold arrays person.json Object holder Key Value { "firstName": "Daniel", "lastName": "Sempre", "age": 24, "isMarried": true } Comma separeted

Nested array of objects JSON Structure Object holder person.json Key Value { "firstName": "John", "lastName": "Snow", "address": { "country": "Spain", "city": "Barcelona", "street": "Barcelona" }, "phoneNumbers": [ "number": "1e341341" "number": "542152" } ] Nested object Nested array of objects Array holder

JSON Data Types Type Description Number double- precision floating-point String double-quoted Unicode Boolean true or false Array an ordered sequence of values Object an unordered collection of key:value pairs null empty

GSON Provide easy to use mechanisms to convert Java to JSON and vice-versa Generate compact and readability JSON output pom.xml <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency>

GSON Initialization Gson objects are responsible for the JSON manipulations GsonBuilder creates an instance of GSON excludeFieldsWithoutExposeAnnotation() – excludes fields without @Expose annotation setPrettyPrinting() – justifies the JSON create() – creates an instance of Gson JsonParser.java Gson gson = new GsonBuilder() .excludeFieldsWithoutExposeAnnotation() .setPrettyPrinting() .create();

Export Single Object to JSON AddressJsonDto.java The filed will be imported/exported public class AddressJsonDto implements Serializable { @Expose private String country; private String city; private String street; } JsonParser.java AddressJsonDto addressJsonDto = new AddressJsonDto(); addressJsonDto.setCountry("Bulgaria"); addressJsonDto.setCity("Sofia"); addressJsonDto.setStreet("Mladost 4"); String content = this.gson.toJson(addressJsonDto); Creates JSON

Export Single Object to JSON JsonParser.java AddressJsonDto addressJsonDto = new AddressJsonDto(); addressJsonDto.setCountry("Bulgaria"); addressJsonDto.setCity("Sofia"); addressJsonDto.setStreet("Mladost 4"); String content = this.gson.toJson(addressJsonDto); address.json { "country": "Bulgaria", "city": "Sofia", "street": "Mladost 4" }

Export Multiple Object to JSON JsonParser.java List<AddressJsonDto> addressJsonDtos = new ArrayList<>(); addressJsonDtos.add(addressJsonDtoBulgaria); addressJsonDtos.add(addressJsonDtoSpain); String content = this.gson.toJson(addressJsonDtos); addresses.json [ { "country": "Bulgaria", "city": "Sofia", "street": "Mladost 4" }, "country": "Spain", "city": "Barcelona", "street": "Las Ramblas" } ]

Import Single Object to JSON AddressJsonDto.java The filed will be imported/exported public class AddressJsonDto implements Serializable { @Expose private String country; private String city; private String street; } JsonParser.java Creates Object AddressJsonDto addressJsonDto = this.gson.fromJson(AddressJsonDto.class, "/files/input/json/address.json");

Import Single Object to JSON AddressJsonDto.java public class AddressJsonDto implements Serializable { @Expose private String country; private String city; private String street; } address.json { "country": "Bulgaria", "city": "Sofia", "street": "Mladost 4" }

Import Multiple Object to JSON JsonParser.java Object Array AddressJsonDto[] addressJsonDtos = this.gson.fromJson(AddressJsonDto[].class, "/files/input/json/addresses.json"); addresses.json [ { "country": "Bulgaria", "city": "Sofia", "street": "Mladost 4" }, "country": "Spain", "city": "Barcelona", "street": "Las Ramblas" } ]

Summary JSON Export JSON Import

JSON https://softuni.bg/courses/databases-advanced-hibernate © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license Attribution: this work may contain portions from "Databases" course by Telerik Academy under CC-BY-NC-SA license © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

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/SoftwareUniversity Software University Forums forum.softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.