JsonMappingException:超出START_ARRAY令牌

时间:2013-10-12 10:22:23

标签: java arrays json jackson

给出以下.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
            ]
    }
]

我准备了两个类来表示包含的数据:

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在[来源:android.content.res.AssetManager$AssetInputStream@416a5850;来源:START_ARRAY标记之外; line:5,column:25]
  (通过参考链:com.example.Location [“center”])

4 个答案:

答案 0 :(得分:67)

杰克逊对象映射器抛出了

JsonMappingException: out of START_ARRAY token异常,因为它期待Object {},而它在响应中找到Array [{}]

可以通过在Object的参数中将Object[]替换为geForObject("url",Object[].class)来解决此问题。 参考文献:

  1. Ref.1
  2. Ref.2
  3. Ref.3

答案 1 :(得分:55)

您的JSON字符串格式错误:center的类型是无效对象的数组。将[]替换为{}周围的JSON字符串中的longitudelatitude,以便它们成为对象:

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

答案 2 :(得分:4)

如上所述,杰克逊对象映射器抛出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);

答案 3 :(得分:1)

我将这个问题排序为从JSONLint.com验证json,然后更正它。这是相同的代码。

String jsonStr = "[{\r\n" + "\"name\":\"New York\",\r\n" + "\"number\": \"732921\",\r\n"+ "\"center\": {\r\n" + "\"latitude\": 38.895111,\r\n"  + " \"longitude\": -77.036667\r\n" + "}\r\n" + "},\r\n" + " {\r\n"+ "\"name\": \"San Francisco\",\r\n" +\"number\":\"298732\",\r\n"+ "\"center\": {\r\n" + "    \"latitude\": 37.783333,\r\n"+ "\"longitude\": -122.416667\r\n" + "}\r\n" + "}\r\n" + "]";

ObjectMapper mapper = new ObjectMapper();
MyPojo[] jsonObj = mapper.readValue(jsonStr, MyPojo[].class);

for (MyPojo itr : jsonObj) {
    System.out.println("Val of name is: " + itr.getName());
    System.out.println("Val of number is: " + itr.getNumber());
    System.out.println("Val of latitude is: " + 
        itr.getCenter().getLatitude());
    System.out.println("Val of longitude is: " + 
        itr.getCenter().getLongitude() + "\n");
}

注意:MyPojo[].class是具有json属性的getter和setter方法的类。

结果:

Val of name is: New York
Val of number is: 732921
Val of latitude is: 38.895111
Val of longitude is: -77.036667
Val of name is: San Francisco
Val of number is: 298732
Val of latitude is: 37.783333
Val of longitude is: -122.416667