使用 Jackson 解析 Json 文件

Parsing Json file with Jackson

我将 return 的 WS 称为 Json 对象,如下所示:

   {
        "id": "salton", 
        "name": "salton", 
    }

我使用

解析没有任何问题
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(jsonStr, Show.class);

那我还有一个WS就是return一个对象列表,如下

{
    "id": "saltonId", 
    "name": "salton", 
},
{
    "id": "elCordeLaCiutat", 
    "name": "elCordeLaCiutat", 
}

我想用

解析
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(jsonStr, List<Show.class>.class);

但是我遇到了编译问题

Multiple markers at this line
    - List cannot be resolved to a variable
    - Syntax error on token ">", byte expected after this 
     token
Type listType = new TypeToken<List<Show>>() {}.getType();
return mapper.readValue(jsonStr, listType.class);

对象列表应按如下方式包装在 []

[
    {
        "id": "saltonId", 
        "name": "salton", 
    },
    {
        "id": "elCordeLaCiutat", 
        "name": "elCordeLaCiutat", 
    }
]

你可以这样解开:

ObjectMapper mapper = new ObjectMapper();
List<Show> shows = Arrays.asList(mapper.readValue(json, Show[].class));