JsonMappingException:超出START_ARRAY令牌

JJD*_*JJD 85 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
            ]
    }
]
Run Code Online (Sandbox Code Playgroud)

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

public class Location {
    public String name;
    public int number;
    public GeoPoint center;
}
Run Code Online (Sandbox Code Playgroud)

...

public class GeoPoint {
    public double latitude;
    public double longitude;
}
Run Code Online (Sandbox Code Playgroud)

为了解析.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;
}
Run Code Online (Sandbox Code Playgroud)

只要我遗漏了center属性,就可以解析所有内容.但是,当我尝试解析地理坐标时,我最终得到以下错误消息:

com.fasterxml.jackson.databind.JsonMappingException:无法
在[Source:android.content.res.AssetManager$AssetInputStream@416a5850;来源:START_ARRAY令牌中反序列化com.example.GeoPoint的实例.line:5,column:25]
(通过引用链:com.example.Location ["center"])

Abh*_*eet 92

JsonMappingException: out of START_ARRAY token杰克逊对象映射器抛出异常,因为它正在期待,Object {}而它却发现了一个Array [{}]响应.

这可以通过更换得到解决ObjectObject[]在参数geForObject("url",Object[].class).参考文献:

  1. 给定1
  2. 给定2
  3. Ref.3

  • 谢谢,基本但必要的信息 (4认同)

Kat*_*ona 66

您的JSON字符串格式错误:类型center是无效对象的数组.替换[]使用{}在JSON字符串中longitude,latitude因此它们将是对象:

[
    {
        "name" : "New York",
        "number" : "732921",
        "center" : {
                "latitude" : 38.895111, 
                "longitude" : -77.036667
            }
    },
    {
        "name" : "San Francisco",
        "number" : "298732",
        "center" : {
                "latitude" : 37.783333, 
                "longitude" : -122.416667
            }
    }
]
Run Code Online (Sandbox Code Playgroud)

  • 我遇到了同样的问题,但它很疯狂,因为在 jackson 2.6.3v 中有一个完全不同的疯狂错误,而 2.9.8v 一切正常。该死,所以这些序列化错误太烦人了。 (2认同)

fre*_*dev 7

如前所述,JsonMappingException: out of START_ARRAY tokenJackson对象映射器会抛出异常,因为它期望an 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;
}
Run Code Online (Sandbox Code Playgroud)

另一方面,如果您没有喜欢的pojo Location,则可以使用:

TypeReference<List<Map<String, Object>>> typeReference = new TypeReference<>() {};
return objectMapper.readValue(inputStream, typeReference);
Run Code Online (Sandbox Code Playgroud)


Atu*_*rma 5

我将这个问题排序为从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");
}
Run Code Online (Sandbox Code Playgroud)

注意: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
Run Code Online (Sandbox Code Playgroud)