--Utilisez Jackson, la bibliothèque JSON de Java
{
"user": {
"name": "Pompom Pudding",
"score": {
"kokugo": "good",
"sansuu": 100
},
"favorites": ["soccer", "puzzle"]
}
}
Vous pouvez obtenir un objet Map en passant Map.class comme deuxième argument d'ObjectMapper # readValue. La création d'un objet est simple, mais la récupération des données souhaitées à partir de l'objet créé est compliquée.
ObjectMapper mapper = new ObjectMapper();
Map obj = mapper.readValue(json, Map.class);
System.out.println(obj);
String name = (String)(((Map)obj.get("user")).get("name"));
System.out.println("name=" + name);
Exemple de sortie.
{user={name=Pompom Pudding, score={kokugo=good, sansuu=100}, favorites=[soccer, puzzle]}}
name=Pompom Pudding
Il peut être transformé en objet en préparant une variable d'instance publique ou un setter getter. Dans ce cas, il n'est pas nécessaire d'écrire l'annotation @ JsonProperty.
import java.util.Map;
public class PublicUserObject {
public Map user;
}
ObjectMapper mapper = new ObjectMapper();
PublicUserObject obj = mapper.readValue(json, PublicUserObject.class);
System.out.println(obj);
String name = (String)obj.user.get("name");
System.out.println("name=" + name);
Exemple de sortie.
PublicUserObject@e6abefac
name=Pompom Pudding
Lors de l'utilisation d'une variable d'instance privée, il est nécessaire de spécifier la clé JSON (nom) à mapper avec @ JsonProperty.
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonProperty;
public class SimpleUserObject {
@JsonProperty("user")
private Map user;
public Map getUser() {
return user;
}
@Override
public String toString() {
return getClass().getName() + ": user:" + user;
}
}
ObjectMapper mapper = new ObjectMapper();
SimpleUserObject obj = mapper.readValue(json, SimpleUserObject.class);
System.out.println(obj);
String name = (String) obj.getUser().get("name");
System.out.println("name=" + name);
Exemple de sortie.
SimpleUserObject: user:{name=Pompom Pudding, score={kokugo=good, sansuu=100}, favorites=[soccer, puzzle]}
name=Pompom Pudding
Si vous rendez la méthode d'accesseur un peu plus ferme, elle sera plus facile à utiliser à partir de classes externes.
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonProperty;
public class AccessorUserObject {
@JsonProperty("user")
private Map user;
public String getName() {
return (String) user.get("name");
}
private Map getScore() {
return (Map) user.get("score");
}
public String getKokugoScore() {
return (String) getScore().get("kokugo");
}
public int getSansuuScore() {
return (int) getScore().get("sansuu");
}
public List getFavorites() {
return (List) user.get("favorites");
}
@Override
public String toString() {
return getClass().getName() +
", getName: " + getName() +
", getKokugoScore: " + getKokugoScore() +
", getSansuuScore: " + getSansuuScore() +
", getFavorites: " + getFavorites();
}
}
ObjectMapper mapper = new ObjectMapper();
AccessorUserObject obj = mapper.readValue(json, AccessorUserObject.class);
System.out.println(obj);
String name = obj.getName();
System.out.println("name=" + name);
Exemple de sortie.
AccessorUserObject, getName: Pompom Pudding, getKokugoScore: good, getSansuuScore: 100, getFavorites: [soccer, puzzle]
name=Pompom Pudding
Préparez une classe et une classe interne pour le mappage des données JSON.
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
public class StructedUserObject {
@JsonProperty("user")
public User user;
public static class User {
public String name;
public Score score;
public List favorites;
@Override
public String toString() {
return getClass().getSimpleName()
+ ": name: " + name
+ ", score: " + score
+ ", favorites: " + favorites;
}
}
public static class Score {
public String kokugo;
public int sansuu;
@Override
public String toString() {
return getClass().getSimpleName()
+ ": kokugo: " + kokugo
+ ", sansuu: " + sansuu;
}
}
@Override
public String toString() {
return getClass().getName() + ": user:" + user;
}
}
ObjectMapper mapper = new ObjectMapper();
StructedUserObject obj = mapper.readValue(json, StructedUserObject.class);
System.out.println(obj);
String name = obj.user.name;
System.out.println("name=" + name);
Exemple de sortie.
StructedUserObject: user:User: name: Pompom Pudding, score: Score: kokugo: good, sansuu: 100, favorites: [soccer, puzzle]
name=Pompom Pudding
$ mvn --version
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-05T04:00:29+09:00)
Maven home: /usr/local/Cellar/maven/3.6.1/libexec
Java version: 11.0.2, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home
Default locale: ja_JP, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.5", arch: "x86_64", family: "mac"
├── pom.xml
├── src
│ └── main
│ └── java
│ └── info
│ └── maigo
│ └── lab
│ └── myjacksonsamples
│ ├── AccessorUserObject.java
│ ├── Main.java
│ ├── PublicUserObject.java
│ ├── SimpleUserObject.java
│ └── StructedUserObject.java
└── user.json
pom.xml
Préparez un fichier pom.xml afin qu'il puisse être construit et exécuté par Maven. Ajoutez les bibliothèques Jackson jackson-core, jackson-databind, jackson-annotations aux dépendances. Ajoutez exec-maven-plugin aux plugins afin de pouvoir exécuter des programmes Java à partir de Maven.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>info.maigo.lab</groupId>
<artifactId>my-jackson-samples</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<!-- $ mvn exec:java -->
<!-- https://www.mojohaus.org/exec-maven-plugin/ -->
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>info.maigo.lab.myjacksonsamples.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.9</version>
</dependency>
</dependencies>
</project>
Une commande pour générer un fichier JAR.
$ mvn package
Une commande pour exécuter l'exemple de programme.
$ mvn exec:java
Main.java
package info.maigo.lab.myjacksonsamples;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws IOException {
String json = Files.readString(Path.of("user.json"), StandardCharsets.UTF_8);
createMappedUserObject(json);
createPublicUserObject(json);
createSimpleUserObject(json);
createAccessorUserObject(json);
createStructedUserObject(json);
}
private static void createMappedUserObject(String json) throws IOException {
ObjectMapper mapper = new ObjectMapper();
Map obj = mapper.readValue(json, Map.class);
System.out.println(obj);
String name = (String) (((Map) obj.get("user")).get("name"));
System.out.println("name=" + name);
}
private static void createPublicUserObject(String json) throws IOException {
ObjectMapper mapper = new ObjectMapper();
PublicUserObject obj = mapper.readValue(json, PublicUserObject.class);
System.out.println(obj);
String name = (String) obj.user.get("name");
System.out.println("name=" + name);
}
private static void createSimpleUserObject(String json) throws IOException {
ObjectMapper mapper = new ObjectMapper();
SimpleUserObject obj = mapper.readValue(json, SimpleUserObject.class);
System.out.println(obj);
String name = (String) obj.getUser().get("name");
System.out.println("name=" + name);
}
private static void createAccessorUserObject(String json) throws IOException {
ObjectMapper mapper = new ObjectMapper();
AccessorUserObject obj = mapper.readValue(json, AccessorUserObject.class);
System.out.println(obj);
String name = obj.getName();
System.out.println("name=" + name);
}
private static void createStructedUserObject(String json) throws IOException {
ObjectMapper mapper = new ObjectMapper();
StructedUserObject obj = mapper.readValue(json, StructedUserObject.class);
System.out.println(obj);
String name = obj.user.name;
System.out.println("name=" + name);
}
}
PublicUserObject.java
package info.maigo.lab.myjacksonsamples;
import java.util.Map;
public class PublicUserObject {
public Map user;
}
SimpleUserObject.java
package info.maigo.lab.myjacksonsamples;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Map;
public class SimpleUserObject {
@JsonProperty("user")
private Map user;
public Map getUser() {
return user;
}
@Override
public String toString() {
return getClass().getName() + ": user:" + user;
}
}
AccessorUserObject.java
package info.maigo.lab.myjacksonsamples;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonProperty;
public class AccessorUserObject {
@JsonProperty("user")
private Map user;
public String getName() {
return (String) user.get("name");
}
private Map getScore() {
return (Map) user.get("score");
}
public String getKokugoScore() {
return (String) getScore().get("kokugo");
}
public int getSansuuScore() {
return (int) getScore().get("sansuu");
}
public List getFavorites() {
return (List) user.get("favorites");
}
@Override
public String toString() {
return getClass().getName() +
", getName: " + getName() +
", getKokugoScore: " + getKokugoScore() +
", getSansuuScore: " + getSansuuScore() +
", getFavorites: " + getFavorites();
}
}
StructedUserObject.java
package info.maigo.lab.myjacksonsamples;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
public class StructedUserObject {
@JsonProperty("user")
public User user;
public static class User {
public String name;
public Score score;
public List favorites;
@Override
public String toString() {
return getClass().getSimpleName()
+ ": name: " + name
+ ", score: " + score
+ ", favorites: " + favorites;
}
}
public static class Score {
public String kokugo;
public int sansuu;
@Override
public String toString() {
return getClass().getSimpleName()
+ ": kokugo: " + kokugo
+ ", sansuu: " + sansuu;
}
}
@Override
public String toString() {
return getClass().getName() + ": user:" + user;
}
}
user.json
{
"user": {
"name": "Pompom Pudding",
"score": {
"kokugo": "good",
"sansuu": 100
},
"favorites": ["soccer", "puzzle"]
}
}
Recommended Posts