Issue with parsing the content from JSON file with Jackson & message- JsonMappingException -Cannot deserialize as out of START_ARRAY token Ask Question

Issue with parsing the content from JSON file with Jackson & message- JsonMappingException -Cannot deserialize as out of START_ARRAY token Ask Question

Given the following .json file:

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

I prepared two classes to represent the contained data:

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

...

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

In order to parse the content from the .json file I use Jackson 2.2.x and prepared the following method:

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: [ソース: android.content.res.AssetManager$AssetInputStream@416a5850; 行: 5、列: 25] の START_ARRAY トークンから com.example.GeoPointのインスタンスをデシリアライズできません
(参照チェーン: com.example.Location["center"] 経由)

ベストアンサー1

JsonMappingException: out of START_ARRAY tokenObject {}Jackson オブジェクト マッパーは を期待していますが、応答で が見つかったため、例外がスローされますArray [{}]

これは、 の引数内でObjectを に置き換えることで解決できます。参考文献:Object[]geForObject("url",Object[].class)

  1. 参照1
  2. 参照2
  3. 参照3

おすすめ記事