stackyyflow
stackyyflow

Reputation: 763

Parse JSON to java object

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.

Upvotes: 0

Views: 183

Answers (1)

bhdrkn
bhdrkn

Reputation: 6702

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.

Upvotes: 2

Related Questions