JSON|**JSON以及AJAX的学习**

JSON以及AJAX的学习 一、编写Controller,用来将集合、对象或日期封装成JSON字符串返回给客户端。 本文采用Jackson的方法进行封装。
首先需要引入相关依赖

com.fasterxml.jackson.core jackson-databind 2.5.0 com.fasterxml.jackson.core jackson-core 2.5.0 com.fasterxml.jackson.core jackson-annotations 2.5.0

下面展示一些 内联代码片
@RequestMapping(value = "https://www.it610.com/jsonLearning")//,produces = {"application/json; charset=UTF-8"} @ResponseBody//将服务器端的对象转换成json对象相应回去 public String jsonLearning() throws JsonProcessingException { User user = new User(); user.setUserId(3); user.setUsername("张三"); user.setPassword("123456"); user.setBirthday(new Date()); //创建一个jackson对象,用来转换json格式jackson的对象映射器 ObjectMapper objectMapper = new ObjectMapper(); String string = objectMapper.writeValueAsString(user); System.out.println(string); return string; //由于使用了jackson的@ResponseBody,这里将string以json字符串格式返回,而不进入视图解析器 }

通过在客户端访问,发现出现了中文乱码问题。可通过以下方法解决
1、修改@RequestMapping(value = https://www.it610.com/article/“/jsonLearning”)
– >
@RequestMapping(value = https://www.it610.com/article/“/jsonLearning”,produces = {“application/json; charset=UTF-8”})
2、从根源上解决,配置SpringMVC的配置文件:

二、对于日期问题,需要如下代码进行控制
@RequestMapping(value = "https://www.it610.com/jsonTime")//,produces = {"application/json; charset=UTF-8"} @ResponseBody//将服务器端的对象转换成json对象相应回去 public String jsonTime() throws JsonProcessingException {ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); objectMapper.setDateFormat(sdf); String string = objectMapper.writeValueAsString(new Date()); System.out.println(string); return string; //由于使用了jackson的@ResponseBody,这里将string以json字符串格式返回,而不进入视图解析器 }

三、为了方便,可以编写JsonUtils
package com.igeek.ssm.utils; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import java.text.SimpleDateFormat; public class JsonUtils {public static String getJson(Object object){ return getJson(object,"yyyy-MM-dd hh-mm-ss"); }private static String getJson(Object object, String datefromat) { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false); SimpleDateFormat sdf = new SimpleDateFormat(datefromat); objectMapper.setDateFormat(sdf); try { return objectMapper.writeValueAsString(object); } catch (JsonProcessingException e) { e.printStackTrace(); } return null; } }

四、jQuery编写
$.ajax({ type: "POST", url: "test.json", data: {username:$("#username").val(), content:$("#content").val()}, dataType: "json", success: function(data){ $('#resText').empty(); //清空resText里面的所有内容 var html = ''; $.each(data, function(commentIndex, comment){ html += '' + comment['username'] + ':【JSON|**JSON以及AJAX的学习**】'; }); $('#resText').html(html); } }); $.post("test.php", { "func": "getNameAndTime" }, function(data){ alert(data.name); // John console.log(data.time); //2pm }, "json");

    推荐阅读