adds json libraries, chaotic TryOut but works

This commit is contained in:
2024-04-20 23:51:13 +02:00
parent 758228098f
commit c9e7483665
5 changed files with 63 additions and 3 deletions

3
.gitignore vendored
View File

@@ -1 +1,2 @@
out/ out/
lib/

2
.idea/howtodata.iml generated
View File

@@ -7,5 +7,7 @@
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="javax.json.api" level="project" />
<orderEntry type="library" name="glassfish.javax.json" level="project" />
</component> </component>
</module> </module>

5
java_src/Address.java Normal file
View File

@@ -0,0 +1,5 @@
package java_src;
public record Address(String street,
String number,
String city) { }

10
java_src/Person.java Normal file
View 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) { }

View File

@@ -1,17 +1,59 @@
package java_src; package java_src;
import javax.json.*;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList;
public class TryOut { public class TryOut {
public static void main(String[] args) { public static void main(String[] args) {
String jsonString;
try { try {
Path path = Paths.get("/home/jan/git-projekte/howtodata/JSON/alice.json"); // Path path = Paths.get("/home/jan/git-projekte/howtodata/JSON/alice.json"); // Absoluter Pfad
System.out.println(FileHelper.readJson(path)); 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) { } catch (IOException e) {
System.err.println(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;
}
} }