我无法从自定义类(Turns)获取泛型类型列表:
val turnsType = TypeToken<List<Turns>>() {}.type
val turns = Gson().fromJson(pref.turns, turnsType)
Run Code Online (Sandbox Code Playgroud)
它说:
cannot access '<init>' it is 'public /*package*/' in 'TypeToken'
Run Code Online (Sandbox Code Playgroud) 这是我的JSON数组: -
[
{
"firstName" : "abc",
"lastName" : "xyz"
},
{
"firstName" : "pqr",
"lastName" : "str"
}
]
Run Code Online (Sandbox Code Playgroud)
我在我的String对象中有这个.现在我想将它转换为Java对象并将其存储在Java对象的List中.例如在Student对象中.我使用下面的代码将其转换为Java对象列表: -
ObjectMapper mapper = new ObjectMapper();
StudentList studentList = mapper.readValue(jsonString, StudentList.class);
Run Code Online (Sandbox Code Playgroud)
我的列表类是: -
public class StudentList {
private List<Student> participantList = new ArrayList<Student>();
//getters and setters
}
Run Code Online (Sandbox Code Playgroud)
我的学生对象是: -
class Student {
String firstName;
String lastName;
//getters and setters
}
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么吗?我得到以下异常: -
Exception : com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.aa.Student out of START_ARRAY token
Run Code Online (Sandbox Code Playgroud) 我将ArrayList转换为JSONArray.我该如何将其转换回来?
最终结果必须是ArrayList.先感谢您.
编辑:
这是我将ArrayList转换为JSONArray的方法:
String string_object= new Gson().toJson(MyArrayList<OBJECT>);
JSONArray myjsonarray = new JSONArray(string_object);
Run Code Online (Sandbox Code Playgroud)