แปลง json String เป็น List‹List‹String› ด้วย Jackson

ฉันมี json String ที่เป็นรายการของรายการสตริง และจำเป็นต้องแปลงเป็นอ็อบเจ็กต์ของคลาส List<List<String>>

ที่นี่ มันบอกวิธีการแปลง จากสตริงไปยังรายการ แต่มันใช้งานไม่ได้สำหรับฉัน เนื่องจากฉันไม่สามารถระบุคลาส List<List<String>> ใน constructCollectionType ได้

นี่คือความพยายามของฉัน ซึ่งไม่ได้ผล:

ObjectMapper objectMapper = new ObjectMapper();
TypeFactory typeFactory = objectMapper.getTypeFactory();
List<List<String>> list = objectMapper.readValue(response,
   typeFactory.constructCollectionType(List.class, List.class));

จะบรรลุเป้าหมายนี้ได้อย่างไร?


person ps0604    schedule 24.02.2019    source แหล่งที่มา
comment
คุณใช้ Jackson เวอร์ชันใด ฉันทดสอบมันใหม่ล่าสุดและทำงานได้อย่างถูกต้อง   -  person Michał Ziober    schedule 24.02.2019


คำตอบ (1)


ใช้ TypeReference<T> : :

TypeReference<List<List<String>>> typeReference = 
        new TypeReference<List<List<String>>>() {};
List<List<String>> list = mapper.readValue(json, typeReference);
person cassiomolin    schedule 24.02.2019