I'm trying to parse JSON to java object by retrieving a list of category item from JSON file but there is an error as shown:
Error:
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
at [Source: java.io.StringReader@6be46e8f; line: 1, column: 1]
Here is my code to retrieve category object:
public ArrayList<Category> getAllTasksFromFile(String jsonString) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
ArrayList<Category> allTasks = mapper.readValue(jsonString, new TypeReference<ArrayList<Category>>() {});
return allTasks;
}
My JSON file that shows 1 category (actual file has many categories):
[ {
"categories" : {
"task" : [ {
"reminder" : 123,
"endDate" : "-1",
"name" : "Do homework",
"description" : "Do it now",
"startTime" : -1,
"endTime" : -1,
"priority" : 5,
"isDone" : false,
"startDate" : "-1"
}, {
"reminder" : 123,
"endDate" : "-1",
"name" : "Do homework",
"description" : "Do it now",
"startTime" : -1,
"endTime" : -1,
"priority" : 5,
"isDone" : false,
"startDate" : "-1"
} ],
"floatTask" : [ {
"reminder" : 123,
"endDate" : "-1",
"name" : "Do homework",
"description" : "Do it now",
"startTime" : -1,
"endTime" : -1,
"priority" : 5,
"isDone" : false,
"startDate" : "-1"
}, {
"reminder" : 123,
"endDate" : "-1",
"name" : "Do homework",
"description" : "Do it now",
"startTime" : -1,
"endTime" : -1,
"priority" : 5,
"isDone" : false,
"startDate" : "-1"
} ],
"event" : [ {
"reminder" : 123,
"endDate" : "-1",
"name" : "Do homework",
"description" : "Do it now",
"startTime" : -1,
"endTime" : -1,
"priority" : 5,
"isDone" : false,
"startDate" : "-1"
}, {
"reminder" : 123,
"endDate" : "-1",
"name" : "Do homework",
"description" : "Do it now",
"startTime" : -1,
"endTime" : -1,
"priority" : 5,
"isDone" : false,
"startDate" : "-1"
} ]
},
"categoryName" : "categories"
} ]
Category.java
public class Category {
private List<Task> task;
private List<Task> floatTask;
private List<Task> event;
public List<Task> getTask() {
return task;
}
public void setTask(List<Task> task) {
this.task = task;
}
public List<Task> getFloat() {
return floatTask;
}
public void setTaskType(List<Task> floatTask) {
this.floatTask = floatTask;
}
public List<Task> getEvent() {
return event;
}
public void setEvent(List<Task> event) {
this.event = event;
}
}
JSON:
@SuppressWarnings("unchecked")
public JSONArray addNewCategory(String categoryName) {
Task newTask = new Task("Do homework", "Do it now", 5, 123, "School", false);
JSONArray category = new JSONArray();
JSONObject categoryObject = new JSONObject();
JSONArray array = new JSONArray();
JSONObject arrayElementOne = new JSONObject();
JSONArray arrayElementOneArray = new JSONArray();
JSONObject arrayElementOneArrayElementOne = convertTaskToJSON(newTask);
JSONObject arrayElementOneArrayElementTwo = convertTaskToJSON(newTask);
arrayElementOneArray.add(arrayElementOneArrayElementOne);
arrayElementOneArray.add(arrayElementOneArrayElementTwo);
arrayElementOne.put("floatTask", arrayElementOneArray);
arrayElementOne.put("task", arrayElementOneArray);
arrayElementOne.put("event", arrayElementOneArray);
array.add(arrayElementOne);
categoryObject.put(categoryName, array);
category.add(categoryObject);
return category;
}
Anyone knows what the problem might be? I need to retrieve the list of categories.
ArrayList<Category>>.ArrayList<Category>, not even close. Describe each nested member of JSON and how it relates to aArrayList<Category>and you will find your problem.