java怎么将json字符串[{"power": 1.0, "windSpeed": 1.0}, {"power": 2.0, "windSpeed": 2.0}]转化为map
时间: 2023-07-21 08:06:16 浏览: 125
你可以使用Java中的JSON库,如Jackson或Gson,来将JSON字符串转换为Map对象。下面是使用Jackson库的示例代码:
```java
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
String jsonString = "[{\"power\": 1.0, \"windSpeed\": 1.0}, {\"power\": 2.0, \"windSpeed\": 2.0}]";
ObjectMapper objectMapper = new ObjectMapper();
try {
List<Map<String, Object>> mapList = objectMapper.readValue(jsonString, new TypeReference<List<Map<String, Object>>>() {});
System.out.println(mapList);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
这段代码使用了Jackson库的ObjectMapper类,将JSON字符串转换为List<Map<String, Object>>对象。其中,List表示JSON数组,Map表示JSON对象,String表示属性名,Object表示属性值。你可以根据需要修改代码以适应其他数据结构或JSON库。
阅读全文
相关推荐



















