2013-10-12 51 views
32

鑑於以下以.json文件:JsonMappingException:出START_ARRAY令牌

[ 
    { 
     "name" : "New York", 
     "number" : "732921", 
     "center" : [ 
       "latitude" : 38.895111, 
       "longitude" : -77.036667 
      ] 
    }, 
    { 
     "name" : "San Francisco", 
     "number" : "298732", 
     "center" : [ 
       "latitude" : 37.783333, 
       "longitude" : -122.416667 
      ] 
    } 
] 

我準備了兩個類來表示所包含的數據:

public class Location { 
    public String name; 
    public int number; 
    public GeoPoint center; 
} 

...

public class GeoPoint { 
    public double latitude; 
    public double longitude; 
} 

爲了解析.json文件中的內容,我使用了Jackson 2.2.x並準備了以下方法:

public static List<Location> getLocations(InputStream inputStream) { 
    ObjectMapper objectMapper = new ObjectMapper(); 
    try { 
     TypeFactory typeFactory = objectMapper.getTypeFactory(); 
     CollectionType collectionType = typeFactory.constructCollectionType(
              List.class, Location.class); 
     return objectMapper.readValue(inputStream, collectionType); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

只要我遺漏了center屬性,所有的內容都可以被解析。然而,當我嘗試分析地理座標我結束了以下錯誤消息:

com.fasterxml.jackson.databind.JsonMappingException:無法反序列化
com.example.GeoPoint的實例出來的START_ARRAY token at [Source:[email protected];行:5,柱:25]
(通過參考鏈:com.example.Location [ 「中心」])

+0

爲什麼你使用傑克森,如果你可以簡單地用json解析器解析它 –

+3

你的JSON字符串格式不正確,'center'類型是一個無效對象數組。嘗試用'經度'和'緯度'附近的JSON字符串中的{'和'}替換'['和']',以便它們成爲對象。 – Katona

+0

@Katona謝謝。您能否將您的評論轉換爲答案,以便我可以解決這個問題? – JJD

回答

31

你的JSON字符串格式錯誤:的center類型是無效對象的數組。圍繞longitudelatitude所以他們會對象JSON字符串替換[]{}

[ 
    { 
     "name" : "New York", 
     "number" : "732921", 
     "center" : { 
       "latitude" : 38.895111, 
       "longitude" : -77.036667 
      } 
    }, 
    { 
     "name" : "San Francisco", 
     "number" : "298732", 
     "center" : { 
       "latitude" : 37.783333, 
       "longitude" : -122.416667 
      } 
    } 
] 
33

JsonMappingException: out of START_ARRAY token異常是由傑克遜的對象映射器,因爲它是在等一個Object {},而它在響應中發現的Array [{}]拋出。

這可以通過在geForObject("url",Object[].class)的參數中將Object替換爲Object[]來解決。 參考文獻:

  1. Ref.1
  2. Ref.2
  3. Ref.3
+0

謝謝,基本但必要的信息 –

+0

這對我有用!我有一個Product.class,我只是添加了Product []。class。巴姆!加工。感謝Abhijeet! – Jonathan

1

至於說,JsonMappingException: out of START_ARRAY token異常是由傑克遜的對象映射器,因爲它是在等一個Object {},而它在響應中發現的Array [{}]拋出。

一個簡單的解決方案可以與被替換的方法getLocations

public static List<Location> getLocations(InputStream inputStream) { 
    ObjectMapper objectMapper = new ObjectMapper(); 
    try { 
     TypeReference<List<Location>> typeReference = new TypeReference<>() {}; 
     return objectMapper.readValue(inputStream, typeReference); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

在另一方面,如果你沒有像Location一個POJO,你可以使用:

TypeReference<List<Map<String, Object>>> typeReference = new TypeReference<>() {}; 
return objectMapper.readValue(inputStream, typeReference); 
+0

感謝您分享此替代實施。你能說出使用'CollectionType'與'TypeReference'是否有優點或缺點嗎? – JJD

+0

'TypeReference'語法比另一個短得多。不確定的是,如果在某些情況下可能存在與類型擦除相關的反指示...但是在我的世界中,「類型參考」更容易使用和理解。 – freedev

相關問題