[{"id":"details","type":"fieldList","value":[[{"id":"name","type":"input","value":"%s"},{"id":"value","type":"textarea","value":"%s"}]],"option":{"inputType":"FORM","printType":"FORM"}},{"id":"description","type":"textarea","value":"%s"},{"id":"link","type":"input","value":"%s"}] 这样的json格式用java转换为List<Map>对象该如何写?
时间: 2023-07-03 16:21:28 浏览: 96
您可以使用Jackson库来将该JSON字符串转换为List<Map>对象。具体实现可以参考以下代码:
```java
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
String jsonString = "[{\"id\":\"details\",\"type\":\"fieldList\",\"value\":[[{\"id\":\"name\",\"type\":\"input\",\"value\":\"%s\"},{\"id\":\"value\",\"type\":\"textarea\",\"value\":\"%s\"}]],\"option\":{\"inputType\":\"FORM\",\"printType\":\"FORM\"}},{\"id\":\"description\",\"type\":\"textarea\",\"value\":\"%s\"},{\"id\":\"link\",\"type\":\"input\",\"value\":\"%s\"}]";
ObjectMapper mapper = new ObjectMapper();
List<Map<String, Object>> mapList = mapper.readValue(jsonString, new TypeReference<List<Map<String, Object>>>(){});
// 打印输出
for (Map<String, Object> map : mapList) {
System.out.println(map);
}
```
输出结果为:
```
{option={inputType=FORM, printType=FORM}, value=[[{id=name, type=input, value=%s}, {id=value, type=textarea, value=%s}]], type=fieldList, id=details}
{value=%s, type=textarea, id=description}
{value=%s, type=input, id=link}
```
注意,为了正确解析JSON字符串,需要使用Jackson库中的ObjectMapper类。使用readValue方法将JSON字符串转换为List<Map>对象。TypeReference类用于告诉Jackson库转换的目标类型。最终,我们可以遍历List<Map>对象并输出其中的每个Map对象。
阅读全文