Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230617
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
javab_20230617
Commits
23ca5000
Commit
23ca5000
authored
Aug 20, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Obsługa JSONa za pomocą jakarta.json
parent
324c19ff
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
103 additions
and
0 deletions
+103
-0
pom.xml
PC33-RestKlient/pom.xml
+17
-0
Klient02_URL_JSON.java
...tKlient/src/main/java/sklep/klient/Klient02_URL_JSON.java
+33
-0
Klient05_HttpClient_JSON.java
.../src/main/java/sklep/klient/Klient05_HttpClient_JSON.java
+53
-0
No files found.
PC33-RestKlient/pom.xml
View file @
23ca5000
...
@@ -21,4 +21,20 @@
...
@@ -21,4 +21,20 @@
</plugin>
</plugin>
</plugins>
</plugins>
</build>
</build>
<dependencies>
<!-- implementacje technilogii Jakarta JSON P - glassfish to starsza wersja, a Eclipse Parsson to nowsza -->
<!--
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.json</artifactId>
<version>2.0.1</version>
</dependency>
-->
<dependency>
<groupId>
org.eclipse.parsson
</groupId>
<artifactId>
parsson
</artifactId>
<version>
1.1.4
</version>
</dependency>
</dependencies>
</project>
</project>
\ No newline at end of file
PC33-RestKlient/src/main/java/sklep/klient/Klient02_URL_JSON.java
0 → 100644
View file @
23ca5000
package
sklep
.
klient
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URL
;
import
jakarta.json.Json
;
import
jakarta.json.JsonArray
;
import
jakarta.json.JsonReader
;
import
jakarta.json.JsonValue
;
public
class
Klient02_URL_JSON
{
public
static
void
main
(
String
[]
args
)
{
try
{
URL
url
=
new
URL
(
Ustawienia
.
ADRES_USLUGI
+
"/products.json"
);
try
(
InputStream
inputStream
=
url
.
openStream
();
JsonReader
reader
=
Json
.
createReader
(
inputStream
))
{
JsonArray
array
=
reader
.
readArray
();
// System.out.println(array);
for
(
JsonValue
jsonValue
:
array
)
{
//System.out.println(jsonValue);
System
.
out
.
println
(
jsonValue
.
asJsonObject
().
getString
(
"productName"
));
System
.
out
.
println
(
" opis: "
+
jsonValue
.
asJsonObject
().
getString
(
"description"
,
""
));
System
.
out
.
println
(
" cena: "
+
jsonValue
.
asJsonObject
().
getJsonNumber
(
"price"
).
bigDecimalValue
());
}
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
PC33-RestKlient/src/main/java/sklep/klient/Klient05_HttpClient_JSON.java
0 → 100644
View file @
23ca5000
package
sklep
.
klient
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
java.net.http.HttpClient
;
import
java.net.http.HttpRequest
;
import
java.net.http.HttpResponse
;
import
java.net.http.HttpResponse.BodyHandlers
;
import
jakarta.json.Json
;
import
jakarta.json.JsonArray
;
import
jakarta.json.JsonObject
;
import
jakarta.json.JsonReader
;
import
jakarta.json.JsonValue
;
public
class
Klient05_HttpClient_JSON
{
public
static
void
main
(
String
[]
args
)
{
try
{
JsonArray
array
=
pobierzJsona
(
Ustawienia
.
ADRES_USLUGI
+
"/products.json"
);
for
(
JsonValue
jsonValue
:
array
)
{
JsonObject
jsonObject
=
jsonValue
.
asJsonObject
();
System
.
out
.
println
(
jsonObject
.
getString
(
"productName"
));
if
(
jsonObject
.
containsKey
(
"description"
))
{
System
.
out
.
println
(
" opis: "
+
jsonObject
.
getString
(
"description"
,
""
));
}
System
.
out
.
println
(
" cena: "
+
jsonObject
.
getJsonNumber
(
"price"
).
bigDecimalValue
());
}
}
catch
(
IOException
|
InterruptedException
|
URISyntaxException
e
)
{
e
.
printStackTrace
();
}
}
private
static
JsonArray
pobierzJsona
(
String
adres
)
throws
IOException
,
InterruptedException
,
URISyntaxException
{
HttpClient
httpClient
=
HttpClient
.
newHttpClient
();
URI
uri
=
new
URI
(
adres
);
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
).
build
();
HttpResponse
<
InputStream
>
response
=
httpClient
.
send
(
request
,
BodyHandlers
.
ofInputStream
());
System
.
out
.
println
(
"response "
+
response
);
System
.
out
.
println
(
"status: "
+
response
.
statusCode
());
System
.
out
.
println
(
"Content-Type: "
+
response
.
headers
().
firstValue
(
"Content-Type"
).
orElse
(
"BRAK"
));
return
wczytajJsona
(
response
.
body
());
}
private
static
JsonArray
wczytajJsona
(
InputStream
input
)
{
try
(
JsonReader
reader
=
Json
.
createReader
(
input
))
{
return
reader
.
readArray
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment