Compare commits

..

3 Commits

7 changed files with 82 additions and 3 deletions

1
.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>

10
.idea/libraries/glassfish_javax_json.xml generated Normal file
View File

@@ -0,0 +1,10 @@
<component name="libraryTable">
<library name="glassfish.javax.json" type="repository">
<properties maven-id="org.glassfish:javax.json:1.1.4" />
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/javax.json-1.1.4.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

10
.idea/libraries/javax_json_api.xml generated Normal file
View File

@@ -0,0 +1,10 @@
<component name="libraryTable">
<library name="javax.json.api" type="repository">
<properties maven-id="javax.json:javax.json-api:1.1.4" />
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/javax.json-api-1.1.4.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

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,58 @@
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);
} }
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;
}
} }