简单的说,就是要实现java对象转化为json对象,下面采取两种实现方法,一种是fastjson解析器,另外一种爱用jackjson
一、采用fastjson结合springmvc的方式
1、引入依赖
<!--锁定所有spring的版本--> <properties> <spring.version>5.0.2.RELEASE</spring.version> <!--编译版本修改--> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.0</version> </dependency> <!--多视图解析器--> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.9.8</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> </dependencies>
2、编写实体类
private String name; private int age; private String password; @JSONField(format = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd") @JsonFormat(pattern = "yyyy-MM-dd") private Date date;
3、编写controller控制代码
/** * 返回java对象 * @param student * @return */ @RequestMapping("/testAjax") @ResponseBody public Student testAjax( Student student) { System.out.println("testAjax is working"); student.setName("tom"); student.setAge(23); student.setPassword("123456"); student.setDate(new Date()); System.out.println(student); return student; }
4、在springmvc中编写配置json解析代码
<!--配置spring开启注解mvc的支持 *配置json格式转化器 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!--annotation-driven:左右如下 1、会自动注册RequestMappingHandlerMapping、RequestMappingHandlerAdapter、ExceptionHandlerExceptionResolver三个bean支持使用了像@RquestMapping、ExceptionHandler等等的注解的controller 方法去处理请求。 2、支持使用了ConversionService的实例对表单参数进行类型转换。 3、支持使用@NumberFormat、@NumberFormat等注解对数据类型进行格式化。 4、支持使用@Valid对javaBean进行JSR-303验证。 5、支持使用@RequestBody、@ResponseBody。 -->
5、在response.jsp中发送ajax请求数据
$.getJSON("/user/testAjax",function (data) { console.log(data); })
测试代码:
二、采用jackjson的方式
1、导入依赖如上相同
2、创建实体类
也就是在之前springmvc中表单提交的时候封装对象的问题,遇见日期类型解决404的问题解决方案一
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date brithday;
private String name; private int age; private String password; @JSONField(format = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd") @JsonFormat(pattern = "yyyy-MM-dd") private Date date;
3、编写测试代码
@Test
public void test1() throws Exception{
Man man = new Man();
man.setName("陈多多");
man.setAddress("重庆合川");
man.setBrithday(new Date());
Man man1 = new Man();
man.setName("陈多多");
man.setAddress("重庆合川");
man.setBrithday(new Date());
Man man2 = new Man();
man.setName("陈多多");
man.setAddress("重庆合川");
man.setBrithday(new Date());
//创建list集合转为json
List<Man> ps = new ArrayList<Man>();
ps.add(man);
ps.add(man1);
ObjectMapper objectMapper = new ObjectMapper();
String value = objectMapper.writeValueAsString(ps);
System.out.println(value);
}
@Test
public void test3() throws Exception{
Map<String, Object> map = new HashMap<String, Object>();
map.put("name","陈多多");
map.put("age",23);
map.put("address","重庆合川");
ObjectMapper objectMapper = new ObjectMapper();
String value = objectMapper.writeValueAsString(map);
System.out.println(value);
}
}
测试结果如下:
list集合转化出来是一个数组对象,map结合转化出来和java对象一致