実現したいこと
jsonの深い階層にあるデータを掘り起こしてマッピングしたいです。
前提
このようなJsonファイルがあるとします。
json
1{ 2 "name": "tarou", 3 "data": { 4 "age": 26 5 } 6}
これをマッピングする際に以下のようなクラスでマッピングを行いたいと思っています。
この中にDataクラスを入れればいいというのは無しでお願いします。
→データの中の"age"だけを取り出したいです。
java
1import lombok.Getter; 2import lombok.Setter; 3import lombok.ToString; 4 5@Getter 6@Setter 7@ToString 8public class Json { 9 private String name; 10 private int age; 11}
発生している問題・エラーメッセージ
この場合、Json内のdataをマッピングするためのプロパティがないというエラーが出ます。
Terminal
1com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "data" (class jam.genshin.testData), not marked as ignorable (2 known properties: "name", "age"]) 2 at [Source: (String)"{"name": "tarou","data": {"age": 26}}"; line: 1, column: 27] (through reference chain: jam.genshin.testData["data"])
該当のソースコード
java
1public class App 2{ 3 public static void main( String[] args ) 4 { 5 String json = "{\"name\": \"tarou\",\"data\": {\"age\": 26}}"; 6 ObjectMapper mapper = new ObjectMapper(); 7 try { 8 testData testData = mapper.readValue(json, testData.class); 9 } catch (JsonMappingException e) { 10 // TODO Auto-generated catch block 11 e.printStackTrace(); 12 } catch (JsonProcessingException e) { 13 // TODO Auto-generated catch block 14 e.printStackTrace(); 15 } 16 } 17}
試したこと
@JsonPropertyアノテーションなど試しましたが、深い階層のプロパティを直接参照することができませんでした。
(私のやり方が間違っていた可能性はあります。)
補足情報(FW/ツールのバージョンなど)
Mavenプロジェクトです
xml
1<?xml version="1.0" encoding="UTF-8"?> 2 3<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>test.test</groupId> 8 <artifactId>test</artifactId> 9 <version>0.1-test</version> 10 11 <name>test</name> 12 13 <properties> 14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 15 <maven.compiler.source>1.7</maven.compiler.source> 16 <maven.compiler.target>1.7</maven.compiler.target> 17 </properties> 18 19 <dependencies> 20 <dependency> 21 <groupId>junit</groupId> 22 <artifactId>junit</artifactId> 23 <version>4.11</version> 24 <scope>test</scope> 25 </dependency> 26 27 <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> 28 <dependency> 29 <groupId>com.fasterxml.jackson.core</groupId> 30 <artifactId>jackson-core</artifactId> 31 <version>2.15.2</version> 32 </dependency> 33 <dependency> 34 <groupId>com.fasterxml.jackson.core</groupId> 35 <artifactId>jackson-annotations</artifactId> 36 <version>2.15.2</version> 37 </dependency> 38 <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> 39 <dependency> 40 <groupId>com.fasterxml.jackson.core</groupId> 41 <artifactId>jackson-databind</artifactId> 42 <version>2.15.2</version> 43 </dependency> 44 45 <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> 46 <dependency> 47 <groupId>org.projectlombok</groupId> 48 <artifactId>lombok</artifactId> 49 <version>1.18.28</version> 50 <scope>provided</scope> 51 </dependency> 52 53 54 55 <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp --> 56 <dependency> 57 <groupId>com.squareup.okhttp3</groupId> 58 <artifactId>okhttp</artifactId> 59 <version>4.11.0</version> 60 </dependency> 61 </dependencies> 62 63 <build> 64 <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> 65 <plugins> 66 <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> 67 <plugin> 68 <artifactId>maven-clean-plugin</artifactId> 69 <version>3.1.0</version> 70 </plugin> 71 <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> 72 <plugin> 73 <artifactId>maven-resources-plugin</artifactId> 74 <version>3.0.2</version> 75 </plugin> 76 <plugin> 77 <artifactId>maven-compiler-plugin</artifactId> 78 <version>3.8.0</version> 79 </plugin> 80 <plugin> 81 <artifactId>maven-surefire-plugin</artifactId> 82 <version>2.22.1</version> 83 </plugin> 84 <plugin> 85 <artifactId>maven-jar-plugin</artifactId> 86 <version>3.0.2</version> 87 </plugin> 88 <plugin> 89 <artifactId>maven-install-plugin</artifactId> 90 <version>2.5.2</version> 91 </plugin> 92 <plugin> 93 <artifactId>maven-deploy-plugin</artifactId> 94 <version>2.8.2</version> 95 </plugin> 96 <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> 97 <plugin> 98 <artifactId>maven-site-plugin</artifactId> 99 <version>3.7.1</version> 100 </plugin> 101 <plugin> 102 <artifactId>maven-project-info-reports-plugin</artifactId> 103 <version>3.0.0</version> 104 </plugin> 105 </plugins> 106 </pluginManagement> 107 </build> 108</project> 109
ここにより詳細な情報を記載してください。
回答2件
あなたの回答
tips
プレビュー
2023/06/23 23:54
2023/06/24 17:36 編集