2016-03-28 71 views
0

我有這樣的代碼:sonMappingException:無法反序列化的java.util.ArrayList實例出來START_OBJECT令牌

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

} 
+0

當你有一個客戶端輸入錯誤,添加異常處理程序並檢查輸入請求和Java類。 – VinayVeluri

+0

它會顯示相同的'JsonMappingException'打印到控制檯否? –

回答

0

它工作時,我已更改爲:

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