JsonMappingException:出START_ARRAY令牌 [英] JsonMappingException: out of START_ARRAY token

查看:1977
本文介绍了JsonMappingException:出START_ARRAY令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下.json文件:

  [
    {
        名:纽约,
        数字:732921,
        中央 : [
                纬度:38.895111,
                东经:-77.036667
            ]
    },
    {
        名:旧金山,
        数字:298732,
        中央 : [
                纬度:37.783333,
                东经:-122.416667
            ]
    }
]
 

我prepared两个类重新present所包含的数据:

 公共类位置{
    公共字符串名称;
    公众诠释号码;
    公众的GeoPoint中心;
}
 

...

 公共类的GeoPoint {
    公众双重纬度;
    公共双经度;
}
 

为了解析从我用的是.json文件中的内容杰克逊的2.2.x 和prepared下面的方法

 公共静态列表<地点>的getLocations(InputStream中的InputStream){
    ObjectMapper objectMapper =新ObjectMapper();
    尝试 {
        TypeFactory typeFactory = objectMapper.getTypeFactory();
        CollectionType collectionType = typeFactory.constructCollectionType(
                                            List.class,Location.class);
        返回objectMapper.readValue(InputStream的,collectionType);
    }赶上(IOException异常E){
        e.printStackTrace();
    }
    返回null;
}
 

只要我离开了中心属性中的所有内容可以被解析。然而,当我尝试分析地理坐标,我结束了以下错误消息:

  

com.fasterxml.jackson.databind.JsonMappingException:无法反序列化
实例     com.example.GeoPoint出START_ARRAY令牌的[来源:android.content.res.AssetManager$AssetInputStream@416a5850;行:5,列:25]
  (通过参考链:com.example.Location [中心])

解决方案

您JSON字符串格式不正确:的中心的型式是无效的对象的数组。替换 [] {} 围绕经度和JSON字符串纬度所以他们会对象:

  [
    {
        名:纽约,
        数字:732921,
        中央 : {
                纬度:38.895111,
                东经:-77.036667
            }
    },
    {
        名:旧金山,
        数字:298732,
        中央 : {
                纬度:37.783333,
                东经:-122.416667
            }
    }
]
 

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

As long as I leave out the center property all content can be parsed. However, when I try to parse the geo-coordinates I end up with the following error message:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of
com.example.GeoPoint out of START_ARRAY token at [Source: android.content.res.AssetManager$AssetInputStream@416a5850; line: 5, column: 25]
(through reference chain: com.example.Location["center"])

解决方案

Your JSON string is malformed: the type of center is an array of invalid objects. Replace [ and ] with { and } in the JSON string around longitude and latitude so they will be objects:

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

这篇关于JsonMappingException:出START_ARRAY令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆