JSON - SoftUni
Download
Report
Transcript JSON - SoftUni
JSON
Exporting and Importing Data
SoftUni Team
Technical Trainers
Software University
http://softuni.bg
Table of Content
1. JSON Export
2. JSON Import
2
Questions
sli.do
#Hibernate
3
4
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
5
JSON Example
Key
person.json
Value
{
"firstName": "Daniel",
"lastName": "Sempre",
"age": 24,
"isMarried": true
}
var person
Variable
Value
= {
firstName: "Daniel",
lastName: "Sempre",
age: 24,
isMarried: true
js object
Property
};
6
JSON Function
JavaScript
Client
person.js
car.js
Java, PHP, C#
JSON
Server
person.json
PersonCont
roller.java
car.json
CarControll
er.java
7
JSON Structure
Data is represented in name/value pairs.
Curly braces hold objects
Square brackets hold arrays
person.json
Object holder
{
Key
"firstName": "Daniel",
"lastName": "Sempre",
"age": 24,
"isMarried": true
Value
Comma separeted
}
8
JSON Structure
Object holder
{
Key
Value
person.json
"firstName": "John",
object
"lastName":Nested
"Snow",
"address": {
"country": "Spain",
"city": "Barcelona",
"street": "Barcelona"
Nested array of objects
},
"phoneNumbers": [
Array holder
{
"number": "1e341341"
},
{
"number": "542152"
}
]
}
9
JSON Data Types
Type
Description
Number
double- precision floatingpoint
String
double-quoted Unicode
Boolean
true or false
Array
an ordered sequence of values
Object
an unordered collection of
key:value pairs
null
empty
10
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>
11
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();
12
Export Single Object to JSON
AddressJsonDto.java
public
The filed will be
classimported/exported
AddressJsonDto implements
Serializable {
@Expose
private String country;
@Expose
private String city;
@Expose
private String street;
}
JsonParser.java
AddressJsonDto addressJsonDto = new AddressJsonDto();
addressJsonDto.setCountry("Bulgaria");
addressJsonDto.setCity("Sofia"); Creates JSON
addressJsonDto.setStreet("Mladost 4");
String content = this.gson.toJson(addressJsonDto);
13
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"
}
14
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"
}
]
15
Import Single Object to JSON
AddressJsonDto.java
public
The filed will be
classimported/exported
AddressJsonDto implements
Serializable {
@Expose
private String country;
@Expose
private String city;
@Expose
private String street;
}
JsonParser.java
Creates Object
AddressJsonDto addressJsonDto =
this.gson.fromJson("/files/input/json/address.json", AddressJsonDto.class);
16
Import Single Object to JSON
AddressJsonDto.java
public class AddressJsonDto
implements Serializable {
@Expose
private String country;
@Expose
private String city;
address.json
{
"country": "Bulgaria",
"city": "Sofia",
"street": "Mladost 4"
}
@Expose
private String street;
}
17
Import Multiple Object to JSON
Object ArrayJsonParser.java
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"
}
]
18
Summary
1. JSON Export
2. JSON Import
19
JDBC
?
https://softuni.bg/courses/
SoftUni Diamond Partners
License
This course (slides, examples, demos, videos, homework, etc.)
is licensed under the "Creative Commons AttributionNonCommercial-ShareAlike 4.0 International" license
Attribution: this work may contain portions from
"Databases" course by Telerik Academy under CC-BY-NC-SA license
22
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 @ YouTube
youtube.com/SoftwareUniversity
Software University Forums – forum.softuni.bg