【Java】Jacksonがパースする文字列のフィールド名にDouble Quoteがついていない状態でもパース可能にする
Jackson
Double-Quote問題
JacksonのObjectMapperでJSON文字列パースしてJavaオブジェクトに変換するとき、フィールド名にダブルクォートがないと怒られてしまいます。
String json = "{user_id: 1, age: 20, item_id: 2, item_name: "hoge"}"; ObjectMapper mapper = new ObjectMapper(); TypeReference<Map<String, String>> type = new TypeReference<Map<String, String>>() {} List<Map<String, String>> list = mapper.readValue(json, type);
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('u' (code 105)): was expecting double-quote to start field name at [Source: {user_id: 1, age: 20, item_id: 2, item_name: "hoge"}; line: 1, column: 4]
そのため、クォートなしのJSONをパースするためには
mapper.configure()
メソッドでFeature.ALLOW_UNQUOTED_FIELD_NAMES
フラグを有効化します。
mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);