Compare commits
4 Commits
758228098f
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 76d2662a2f | |||
| 1939cd990e | |||
| 120859a8d2 | |||
| c9e7483665 |
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>
|
</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
10
.idea/libraries/glassfish_javax_json.xml
generated
Normal 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
10
.idea/libraries/javax_json_api.xml
generated
Normal 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
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,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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
38
python_src/try_out.py
Normal file
38
python_src/try_out.py
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
class Address:
|
||||||
|
def __init__(self, street, number, city):
|
||||||
|
self.street = street
|
||||||
|
self.number = number
|
||||||
|
self.city = city
|
||||||
|
|
||||||
|
class Person:
|
||||||
|
def __init__(self, name, age, email, address, hobbies, friends):
|
||||||
|
self.name = name
|
||||||
|
self.age = age
|
||||||
|
self.email = email
|
||||||
|
self.address = address
|
||||||
|
self.hobbies = hobbies
|
||||||
|
self.friends = friends
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.name + ', ' + str(self.age) + ', ' + self.email
|
||||||
|
|
||||||
|
file = open('../JSON/alice.json')
|
||||||
|
json_person = json.load(file)
|
||||||
|
file.close()
|
||||||
|
|
||||||
|
json_address = json_person['address']
|
||||||
|
|
||||||
|
python_address = Address(json_address['street'], json_address['number'], json_address['city'])
|
||||||
|
python_person = Person(json_person['name'],
|
||||||
|
json_person['age'],
|
||||||
|
json_person['email'],
|
||||||
|
python_address,
|
||||||
|
json_person['hobbies'],
|
||||||
|
json_person['friends'])
|
||||||
|
|
||||||
|
print(python_person)
|
||||||
|
print('Friends with: ')
|
||||||
|
for friend in python_person.friends:
|
||||||
|
print(friend)
|
||||||
Reference in New Issue
Block a user