一、导入jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
二、Map转实体类
Map map = new HashMap();
map.put(“username”, “张三”);
map.put(“password”, “123456”);
JSON json = (JSON) JSON.toJSON(map);
Student student = (Student)JSONObject.toJavaObject((JSON)JSONObject.toJSON(map), Student.class);
打印内容
System.out.println(“json”+ json);
System.out.println(“student”+student)
json:{“password”:”123456″,”username”:”张三”}
student:com.houz.emtity.Student@1175e2db
三、实体类转Map
Student s = new Student();
s.setName(“李四”);
s.setId(“002”);
s.setAge(12);
JSON jsons = (JSON)JSON.toJSON(s);
Map paramMap = (Map)JSONObject.parseObject(jsons.toString());
打印内容是
System.out.println(“jsons:”+jsons);
paramMap.put(“nick”, “就是”);
System.out.println(“paramMap”+paramMap);
jsons:{“name”:”李四”,”id”:”002″,”age”:12}
paramMap{“nick”:”就是”,”name”:”李四”,”id”:”002″,”age”:12}