一、所需jar包:
jackson-core-x.x.x-rc4.jar、jackson-databind-x.x.x-rc4.jar、jackson-annotations-x.x.x-rc4.jar(本例使用jackson-core-2.6.0-rc4.jar、jackson-databind-2.6.0-rc4.jar、jackson-annotations-2.6.0-rc4.jar)。
二、解析转化:
1、json字符串 < ------ > js
String str="{\"name\" : \"peter\",\"age\" : 25,\"address\" : \"广州\",\"weight\" : \"55.5\",\"handsome\" : true}"; //json字符串 ------> json ObjectMapper mapper=new ObjectMapper(); JsonNode jsonNode=mapper.readTree(str); //json ------>json字符串 String jsonStr=jsonNode.toString();
2、java实体类 < ------ > json
public class User { private String name; private int age; private String address; private double weight; private boolean handsome; //此处省略构造方法和get、set方法
User user=new User("peter", 20, "广州", 55.5, true); ObjectMapper mapper=new ObjectMapper(); //java实体类 ------> json字符串 String jsonStr=mapper.writeValueAsString(user); //json字符串------>json JsonNode json=mapper.readTree(jsonStr); //json ------> java实体类 User user2=mapper.readValue(jsonStr, User.class);
3、List < ------ > json
Listlist=new ArrayList (); list.add("QQQQQQ"); list.add("WWWWWW"); list.add("EEEEEE"); list.add("RRRRRR"); ObjectMapper mapper=new ObjectMapper(); //List ------> json字符串 String jsonStr=mapper.writeValueAsString(list); //json字符串 ------> json JsonNode jsonNode=mapper.readTree(jsonStr); //json字符串 ------> List List list2=mapper.readValue(jsonStr, List.class);
4、Map < ------ > json
Mapmap=new HashMap (); map.put("name", "peter"); map.put("sex", "Man"); map.put("age", "20"); ObjectMapper mapper=new ObjectMapper(); //Map ------> json字符串 String jsonStr=mapper.writeValueAsString(map); //json字符串 ------> json JsonNode json=mapper.readTree(jsonStr); //json ------> Map Map map2=mapper.readValue(json.toString(), Map.class);
5、Set < ------ > json
Setset=new HashSet (); set.add("QQQQQQ"); set.add("wwwwww"); set.add("eeeeee"); ObjectMapper mapper=new ObjectMapper(); //Set ------> json字符串 String jsonStr=mapper.writeValueAsString(set); //json字符串 ------> json JsonNode jsonNode=mapper.readTree(jsonStr); //json字符串 ------> Set Set set2=mapper.readValue(jsonStr, Set.class);
三、CRUD:
1、查询数据:
//peter String name=jsonNode.get("name").asText(); //"peter" String name1=jsonNode.get("name").toString(); //提取handsome(true) JsonNode handsome=jsonNode.path("handsome"); int age=jsonNode.get("age").asInt(); double weight=jsonNode.get("weight").asDouble(); boolean handsome=jsonNode.get("handsome").asBoolean()
for (int i = 0; i < jsonNode.size(); i++) { System.out.println(jsonNode.get(i)); }
2、增加数据:
ObjectMapper mapper=new ObjectMapper(); ObjectNode node=mapper.createObjectNode(); //{"name":"DJ","age":18} node.put("name", "DJ"); node.put("age", 18);
ArrayNode arrayNode=mapper.createArrayNode(); //["name","age"] arrayNode.add("name"); arrayNode.add("age");
3、删除数据:
ObjectMapper mapper=new ObjectMapper(); ObjectNode node=mapper.createObjectNode(); node.put("name", "DJ"); node.put("age", 18); node.remove("name");
ArrayNode arrayNode=mapper.createArrayNode(); //["name","age"] arrayNode.add("name"); arrayNode.add("age"); int index=0; arrayNode.remove(index);
4、修改数据:
ObjectMapper mapper=new ObjectMapper(); ObjectNode node=mapper.createObjectNode(); node.put("name", "DJ"); node.put("age", 18); //重置数据 node.put("name", "MDJ");
ArrayNode arrayNode=mapper.createArrayNode(); //["name","age"] arrayNode.add("name"); arrayNode.add("age"); for (int i = 0; i < arrayNode.size(); i++) { if("name".equals(arrayNode.get(i).asText())){ arrayNode.remove(i); arrayNode.add("userName"); } }