adds json libraries, chaotic TryOut but works
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
out/
|
||||
out/
|
||||
lib/
|
||||
2
.idea/howtodata.iml
generated
2
.idea/howtodata.iml
generated
@@ -7,5 +7,7 @@
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="javax.json.api" level="project" />
|
||||
<orderEntry type="library" name="glassfish.javax.json" level="project" />
|
||||
</component>
|
||||
</module>
|
||||
5
java_src/Address.java
Normal file
5
java_src/Address.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package java_src;
|
||||
|
||||
public record Address(String street,
|
||||
String number,
|
||||
String city) { }
|
||||
10
java_src/Person.java
Normal file
10
java_src/Person.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package java_src;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public record Person(String name,
|
||||
int age,
|
||||
String email,
|
||||
Address address,
|
||||
ArrayList<String> hobbies,
|
||||
ArrayList<String> friends) { }
|
||||
@@ -1,17 +1,59 @@
|
||||
package java_src;
|
||||
|
||||
import javax.json.*;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class TryOut {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String jsonString;
|
||||
try {
|
||||
Path path = Paths.get("/home/jan/git-projekte/howtodata/JSON/alice.json");
|
||||
System.out.println(FileHelper.readJson(path));
|
||||
// Path path = Paths.get("/home/jan/git-projekte/howtodata/JSON/alice.json"); // Absoluter Pfad
|
||||
Path path = Paths.get("JSON/alice.json"); // Besser: relativer Pfad
|
||||
jsonString = FileHelper.readJson(path);
|
||||
System.out.println("Aus Datei eingelesener Json-String:");
|
||||
System.out.println(jsonString);
|
||||
} catch (IOException e) {
|
||||
System.err.println(e);
|
||||
jsonString = "{}";
|
||||
}
|
||||
JsonReader jsonReader = Json.createReader(new StringReader(jsonString));
|
||||
JsonObject ali = jsonReader.readObject();
|
||||
jsonReader.close();
|
||||
System.out.println("In JsonObject verwandelt:");
|
||||
System.out.println(ali.toString());
|
||||
System.out.println("Aus JsonObject erzeugtes Java-Record:");
|
||||
Person a1 = aliceAutomatically(ali);
|
||||
System.out.println(a1);
|
||||
System.out.println("Manuell erzeugtes Java-Record:");
|
||||
}
|
||||
|
||||
private static Person aliceAutomatically(JsonObject ali) {
|
||||
ArrayList<String> hob = new ArrayList<>();
|
||||
for (JsonValue hobby : ali.getJsonArray("hobbies")) {
|
||||
JsonString jsonhobby = (JsonString) hobby;
|
||||
hob.add(jsonhobby.getString());
|
||||
}
|
||||
ArrayList<String> fri = new ArrayList<>();
|
||||
for (JsonValue friend : ali.getJsonArray("friends")) {
|
||||
JsonString jsonFriend = (JsonString) friend;
|
||||
fri.add(jsonFriend.getString());
|
||||
}
|
||||
JsonObject jasonAddr = ali.getJsonObject("address");
|
||||
Address javaAddr = new Address(jasonAddr.getString("street"),
|
||||
jasonAddr.getString("number"),
|
||||
jasonAddr.getString("city"));
|
||||
Person aliAuto = new Person(ali.getString("name"),
|
||||
ali.getInt("age"),
|
||||
ali.getString("email"),
|
||||
javaAddr,
|
||||
hob,
|
||||
fri);
|
||||
return aliAuto;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user