sonMappingException:无法从START_OBJECT标记中反序列化java.util.ArrayList的实例

时间:2016-03-28 10:21:23

标签: java json jackson

我有这段代码:

    final List<Device> devicesList = jsonFileHandlerDevice.getList();

和这个方法:

 @Override
    public List<T> getList() {
        List<T> t = null;
        ObjectMapper mapper = new ObjectMapper();

        if (!file.exists()) {
            return null;
        } else {
            try {
                t = mapper.readValue(file, new TypeReference<List<T>>(){});
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        return t;
    }

为什么我会收到此错误?

rg.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
 at [Source: /Users/eladb/WorkspaceQa/java/MobileAutomationWebService/library-services/src/main/resources/devices.json; line: 1, column: 1]
    at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
    at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219)
    at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:212)
    at org.codehaus.jackson.map.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:246)
    at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:204)
    at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:194)
    at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:30)
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2723)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1815)
    at com.waze.automation.client.services.web.lib.utils.io.JsonFileHandler.getContent(JsonFileHandler.java:66)

使用此对象

public class Device {

    public MobileOs mobileOs;
    public double osVersion;
    public int allocatedPort; //0 for free
//    public Integer index;
    public boolean hasSim;
    public String uuid;

和json文件:

{

    {
      "mobileOs": "ios",
      "osVersion": 4.2,
      "allocatedPort": 4444,
      "hasSim": false,
      "uuid": "uuid2"
    },
   {
      "mobileOs": "Android",
      "osVersion": 5.5,
      "allocatedPort": 5555,
      "hasSim": false,
      "uuid": "uuid1"
    }

}

1 个答案:

答案 0 :(得分:0)

当我改为:

时,它有效
[
    {
      "mobileOs": "ios",
      "osVersion": 4.2,
      "allocatedPort": 4444,
      "hasSim": false,
      "uuid": "uuid2"
    },
  {
    "mobileOs": "Android",
    "osVersion": 5.5,
    "allocatedPort": 5555,
    "hasSim": false,
    "uuid": "uuid1"
  }
  ]