【问题标题】:sonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT tokensonMappingException:无法从 START_OBJECT 令牌中反序列化 java.util.ArrayList 的实例
【发布时间】:2016-03-28 10:21:23
【问题描述】:

我有这个代码:

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

}

【问题讨论】:

  • 当您遇到客户端输入错误时,添加异常处理程序并检查输入请求和 Java 类。
  • 它将显示与控制台编号相同的JsonMappingException?

标签: java json jackson


【解决方案1】:

当我改为:

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

【讨论】:

    猜你喜欢
    • 2014-01-17
    • 2021-03-26
    • 2017-09-22
    • 2020-02-20
    • 2018-02-03
    • 2019-10-16
    • 1970-01-01
    相关资源
    最近更新 更多