質問内容
API(http://geoapi.heartrails.com/api.html#postal)のJSONを読み込みたいと考えているのですが、なぜかエラーがでています。
どこの部分が間違っているのでしょうか。
ご教授頂ければ幸いです。
エラー
Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "response" (class com.tiscon.service.PostalCodeService), not marked as ignorable (0 known properties: ]) at [Source: (URL); line: 1, column: 14] (through reference chain: com.tiscon.service.PostalCodeService["response"])
APIから帰ってくるJSON
Json
1{ 2 "response": { 3 "location": [ 4 { 5 "city": "札幌市北区", 6 "city_kana": "さっぽろしきたく", 7 "town": "北十条西一丁目", 8 "town_kana": "きた10じょうにし1ちょうめ", 9 "x": "141.352625", 10 "y": "43.073528", 11 "prefecture": "北海道", 12 "postal": "0010010" 13 }, 14 { 15 "city": "札幌市北区", 16 "city_kana": "さっぽろしきたく", 17 "town": "北十条西二丁目", 18 "town_kana": "きた10じょうにし2ちょうめ", 19 "x": "141.351328", 20 "y": "43.073307", 21 "prefecture": "北海道", 22 "postal": "0010010" 23 }, 24 { 25 "city": "札幌市北区", 26 "city_kana": "さっぽろしきたく", 27 "town": "北十条西三丁目", 28 "town_kana": "きた10じょうにし3ちょうめ", 29 "x": "141.349677", 30 "y": "43.073127", 31 "prefecture": "北海道", 32 "postal": "0010010" 33 }, 34 { 35 "city": "札幌市北区", 36 "city_kana": "さっぽろしきたく", 37 "town": "北十条西四丁目", 38 "town_kana": "きた10じょうにし4ちょうめ", 39 "x": "141.348077", 40 "y": "43.07288", 41 "prefecture": "北海道", 42 "postal": "0010010" 43 } 44 ] 45 } 46}
Javaコード
PostalCodeService.java
Java
1package com.tiscon.service; 2 3import java.io.IOException; 4import java.net.URL; 5 6import com.fasterxml.jackson.core.JsonParseException; 7import com.fasterxml.jackson.databind.DeserializationFeature; 8import com.fasterxml.jackson.databind.JsonMappingException; 9import com.fasterxml.jackson.databind.ObjectMapper; 10import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; 11 12 13public class PostalCodeService { 14 public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException { 15 Response response = new Response(); 16 17 ObjectMapper mapper = new ObjectMapper(); 18 mapper.registerModule(new JavaTimeModule()); 19 mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); 20 21 URL url = new URL("http://geoapi.heartrails.com/api/json?method=searchByPostal&postal=0010010"); 22 23 response = mapper.readValue(url, Response.class); 24 // System.out.println(mapper.readValue(url, PostalCodeService.class)); 25 } 26}
Response.java
Java
1package com.tiscon.service; 2 3import java.util.List; 4 5public class Response { 6 public List<Location> location; 7}
LocationList.java
Java
1package com.tiscon.service; 2import java.util.List; 3 4public class LocationList { 5 List<Location> location; 6} 7
Location.java
Java
1package com.tiscon.service; 2 3public class Location { 4 public String prefecture; 5 public String city; 6 public String city_kana; 7 public String town; 8 public String town_kana; 9 public float x; 10 public float y; 11 public String postal; 12}
回答2件
あなたの回答
tips
プレビュー