将JSON解析为java对象

时间:2015-09-20 19:26:24

标签: java json

我正在尝试通过从JSON文件中检索类别项列表来解析JSON到java对象,但是出现了错误,如下所示:

错误:

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]

以下是我检索类别对象的代码:

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;

}

我的JSON文件显示1个类别(实际文件有很多类别):

      [ {
  "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;
    }

任何人都知道问题可能是什么?我需要检索类别列表。

1 个答案:

答案 0 :(得分:2)

首先你需要一个包含类别列表的包装器。并且您的方法应该期望该包装器的列表。

这是包装器

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;
    }
}

这是你的新方法。

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

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

<强>解释

现在,让我解释一下你的JSON。您的JSON包含一组对象。此对象具有属性名称category

[ {
  "category" : ...
} ]

我们定义了CategoryWrapper来表示该对象。根据你的JSON category也是一个对象数组。然后创建Category来表示该对象。

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

Category有三个名为taskfloatTaskevent的不同属性。每个属性也由对象数组表示。

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

此对象在java中定义为Task。每个Task也有一些属性,如提醒,endDate等。

[ {
  "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":[{...},{...}...]
                },
                {...}
               ]
} ]

每个都可以显示为java对象。您不需要创建更多类。