0

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.

4
  • Please describe how you expect to map your JSON to a ArrayList<Category>>. Commented Sep 20, 2015 at 19:35
  • I'm expecting a list of Categories (with different category name), where each category contains 3 types of task lists - task, floatTask and event. Each list contains object called Task. I've updated my JSON code for your reference; as I suspect that it might be wrong. Apologies as today is my first time learning JSON Commented Sep 20, 2015 at 19:39
  • The problem is that your JSON does not map to a ArrayList<Category>, not even close. Describe each nested member of JSON and how it relates to a ArrayList<Category> and you will find your problem. Commented Sep 20, 2015 at 19:41
  • Similiar issue with me Insert JSON data to Mysql using Spring Web MVC Commented May 20, 2016 at 19:55

1 Answer 1

2

First you need a wrapper which contains category list. And your method should expect list of that wrapper.

Here is the wrapper

import java.util.List;

public class CategoryWrapper {

    private List<Category> category;

    public List<Category> getCategory() {
        return category;
    }

    public void setCategory(List<Category> category) {
        this.category = category;
    }
}

And here is the your new method.

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    ArrayList<CategoryWrapper> allTasks = mapper.readValue(jsonString, new TypeReference<ArrayList<CategoryWrapper>>() {});

Explanation

Now, let me explain your JSON. Your JSON contains an array of objects. This object has a property name category.

[ {
  "category" : ...
} ]

We defined our CategoryWrapper to represent that object. According to your JSON category is also an array of objects. And you create Category to represent that object.

[ {
  "category" : [{...},{...}...]
} ]

Category has three different property named task, floatTask and event. Each of properties is also represented by array of object.

[ {
  "category" : [{
                  "task":[{...},{...}...],
                  "floatTask":[{...},{...}...],
                  "event":[{...},{...}...]
                },
                {...}
               ]
} ]

This object is defined as Task in java. Each Task is also has some properties like reminder, endDate etc.

[ {
  "category" : [{
                  "task":[{
                        "reminder" : 123,
                        "endDate" : "-1",
                        "name" : "Do homework",
                        "description" : "Do it now",
                        "startTime" : -1,
                        "endTime" : -1,
                        "priority" : 5,
                        "isDone" : false,
                        "startDate" : "-1"
                   }],
                  "floatTask":[{...},{...}...],
                  "event":[{...},{...}...]
                },
                {...}
               ]
} ]

Each of them can be shown as java objects. You do not need to create more classes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.