java 将["Stellantis","VW","BMW","Ford"]转换为List
时间: 2023-07-24 15:14:24 浏览: 42
要将`["Stellantis","VW","BMW","Ford"]`转换为`List`,你可以使用`Jackson`库进行解析。以下是一个示例代码:
```java
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.List;
public class JsonToListExample {
public static void main(String[] args) {
String json = "[\"Stellantis\",\"VW\",\"BMW\",\"Ford\"]";
ObjectMapper objectMapper = new ObjectMapper();
try {
List<String> list = objectMapper.readValue(json, new TypeReference<List<String>>() {});
System.out.println(list);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
在上面的示例中,我们使用`ObjectMapper`类从JSON字符串中读取内容,并将其解析为`List<String>`对象。输出结果将会是`[Stellantis, VW, BMW, Ford]`。
请确保在运行代码之前,你已经添加了`Jackson`库的依赖。