2016-11-30 1 views
1

私はjsonタイプとして1つのカラムを持つpostgresdbからデータを読み出そうとしています。一方、他の列はjsonではありません。私はいくつかの例とstackoverflowコードを使用してみましたが、エラーが発生します。私はこのjavaのpostgresから非json列と共にjson列を取得して使用するにはどうすればよいですか?

ObjectMapper mapper = new ObjectMapper(); 
String json = (rs.getString("topic_data")); 
Map<String, Object> map = new HashMap<String, Object>(); 
// convert JSON string to Map 
map = mapper.readValue(json, new TypeReference<Map<String, String>>() {}); 

そして、このエラーを取得してみました:

Dao com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token 
at [Source: {"topicHeader": "attendant windows", "words": [{"percentage": 0.1322331750852289, "word": "windows"}, {"percentage": 0.07222296435182111, "word": "get"}, {"percentage": 0.07221384639035615, "word": "computer"}, {"percentage": 0.07221264897351826, "word": "instead"}, {"percentage": 0.0722068233704654, "word": "just"}, {"percentage": 0.07202521418513595, "word": "still"}, {"percentage": 0.07181489746918468, "word": "attendant"}, {"percentage": 0.012222238496667995, "word": "don"}, {"percentage": 0.012126611197220477, "word": "really"}, {"percentage": 0.012109084263926815, "word": "ie"}, {"percentage": 0.012104725488616788, "word": "went"}, {"percentage": 0.01210188511739723, "word": "order"}, {"percentage": 0.012101477029454613, "word": "service"}, {"percentage": 0.012100753482874852, "word": "deal"}, {"percentage": 0.012100113702598862, "word": "professional"}, {"percentage": 0.012099577741692897, "word": "store"}, {"percentage": 0.012099153533334564, "word": "purchase"}, {"percentage": 0.01209459547306794, "word": "website"}, {"percentage": 0.01209311873816971, "word": "make"},  {"percentage": 0.01208934633126715, "word": "buy"}]}; line: 1, column: 36]  (through reference chain: java.util.LinkedHashMap["words"])` 

私のJSONの列は次のようである:

{ 
    "topicHeader":"header", 
    "words":[{ 
     "word":"hello", 
     "percentage":12.14 
    },{ 
     "word":"hi", 
     "percentage":22.14 
    }] 
} 

いただきました問題を解決する最良の方法は?

+0

注意を使用します(https://stackoverflow.com/search?q = Can + not + deserialize +インスタンス+ + java.lang.String + out + + START_ARRAY +トークン)おそらくあなたを助けてくれたでしょう –

+0

Ok ..うーん。それは助けたかもしれない...しかし、まだおかげさまです。 – MANU

答えて

0

メッセージに記載されているとおり、[{ "word":"hello", "percentage":12.14 },{ "word":"hi", "percentage":22.14 }]は文字列ではありません。

ですから、適切なカスタムクラスを使用する必要があります。Foo foo = mapper.readValue(json, Foo.class)その後、

class Foo { 
    private String topicHeader; 
    private List<Bar> words; 
    // getter, setter, etc 
} 

class Bar { 
    private String word; 
    private double percentage; 
    // getter, setter, etc 
} 

と、[サイト上のクイック検索]というfoo

関連する問題