用Jackson 反序列包含有数组的Json

http://wiki.fasterxml.com/JacksonInFiveMinutes
官方教程.


public class Test {
	public static void main(String[] args) throws JsonParseException,
			JsonMappingException, IOException {
		
		String jsonTest = "{ \"groupOp\" :\"AND\",\"rules\":[{ \"field\":\"lastName\",\"op\":\"cn\",\"data\":\"Test\"},{\"field\":\"firstName\",\"op\":\"cn\",\"data\":\"Test2222\"}]}";
		
		ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
		
		JsonNode rootNode = mapper.readValue(jsonTest, JsonNode.class);
		
		//获得结点为rules的集合
		JsonNode nameNode = rootNode.path("rules");
		
		//获得数据为groupOp的集合 如果不是集合则返回一个字符串
		JsonNode nameNode2 = rootNode.path("groupOp");
		
		System.out.println("rules:" + nameNode);
		System.out.println("groupOp:" + nameNode2);
		
		//返回rulues下field对应的数组
		System.out.println("field:" + nameNode.findValues("field"));
		
		System.out.println("op:" + nameNode.findValues("op"));
		
		System.out.println("data:" + nameNode.findValues("data"));

	}

}

你可能感兴趣的:(json)