【发布时间】:2017-12-12 18:20:01
【问题描述】:
我就是无法解决这个问题。我从服务器获取数据,将它们保存在共享首选项key = datavalue = "[.. json]。
数据的结构和类型可以在截图中看到。
如何在List <User ..> 中正确地json。我使用这段代码,它使用 jackson2 库,但它不起作用。我收到一个错误
JsonMappingException:无法从 START_ARRAY 令牌中反序列化 com.example.dmitriysamoilov.filipapp.model.UserContactListModel 的实例
public List<UserContactListModel> getUserLocalContactsData() {
List<UserContactListModel> listModels = new ArrayList<>();
String json = "";
sharedPreferences = context.getSharedPreferences(ReservedName.USER_LOCAL_CONTACTS_DATA,
context.MODE_PRIVATE);
if (sharedPreferences.contains("json")) {
json = sharedPreferences.getString("json", "");
}else return listModels;
ObjectMapper mapper = new ObjectMapper();
try {
// Convert JSON string from file to Object
UserContactListModel l = mapper.readValue(json,UserContactListModel.class);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return listModels;
}
我明白这个逻辑是不正确的,但我想不出来很长时间
【问题讨论】:
-
将 {UserContactListModel.class} 替换为 {new TypeReference
- >(){}}