Download presentation
Presentation is loading. Please wait.
1
Exporting and Importing Data
JSON Exporting and Importing Data Databases Frameworks SoftUni Team Technical Trainers Software University © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
2
Table of Content JSON Export JSON Import
© Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
3
Questions sli.do #Hibernate
4
JSON
5
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
6
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
7
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
8
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
9
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
10
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
11
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>
12
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();
13
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
14
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" }
15
Export Multiple Object to JSON
JsonParser.java List<AddressJsonDto> addressJsonDtos = new ArrayList<>(); addressJsonDtos.add(addressJsonDtoBulgaria); addressJsonDtos.add(addressJsonDtoSpain); String content = this.gson.toJson(addressJsonDto); addresses.json [ { "country": "Bulgaria", "city": "Sofia", "street": "Mladost 4" }, "country": "Spain", "city": "Barcelona", "street": "Las Ramblas" } ]
16
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("/files/input/json/address.json", AddressJsonDto.class);
17
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" }
18
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" } ]
19
Summary JSON Export JSON Import
20
JDBC https://softuni.bg/courses/
© Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
21
SoftUni Diamond Partners
22
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 – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
23
Free Trainings @ Software University
Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software Facebook facebook.com/SoftwareUniversity Software YouTube youtube.com/SoftwareUniversity Software University Forums – forum.softuni.bg © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.