反序列化List
(1)使用jackson-mapper
- @Test
- public void test_2(){
- String json="[{\"name\":\"whuang\"},{\"age\":\"abc\"}]";
- ObjectMapper mapper = new ObjectMapper();
- try {
- List list= mapper.readValue(json, List.class);
- System.out.println(list.size());
- System.out.println(list.get(0) instanceof Map);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
pom.xml:
- <!-- Json转化模块 -->
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-mapper-lgpl</artifactId>
- <version>1.9.9</version>
- </dependency>
(2)使用json-lib
- //反序列化
- @Test
- public void test_3(){
- String json="[{\"name\":\"whuang\"},{\"age\":\"abc\"}]";
- JSONArray js = JSONArray.fromObject(json);
- List jRpcReq = (List) JSONArray.toCollection(js,Map.class);
- System.out.println(jRpcReq.size());
- System.out.println(jRpcReq.get(0) instanceof Map);
- }
pom.xml:
- <dependency>
- <groupId>net.sf.json-lib</groupId>
- <artifactId>json-lib</artifactId>
- <version>2.4</version>
- <classifier>jdk15</classifier>
- </dependency>