在日常的java项目开发中,JSON的使用越来越频繁,对于Json的处理工具也有很多。作为阿里开源的一个高性能的JSON框架FastJson很受大家的欢迎,那fastjson是什么框架?下面来我们就来给大家讲解一下这方面的内容。
fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean。
FastJson的特点:
1) FastJson数据处理速度快,无论序列化(把JavaBean对象转化成Json格式的字符串)和反序列化(把JSON格式的字符串转化为Java Bean对象),都是当之无愧的fast;
2) 功能强大(支持普通JDK类,包括javaBean, Collection, Date 或者enum);
3) 零依赖(没有依赖其他的任何类库);
FastJson的简单说明:
FastJson对于JSON格式的字符串的解析主要是用到了下面三个类:
1) JSON:FastJson的解析器,用于JSON格式字符串与JSON对象及JavaBean之间的转化。也是最基础的一个类,因为看过源码之后会发现,下面的两个类继承了JSON类,其中很多方法的实现也是基于JSON类中的parse()方法。
2) JSONObject: FastJson提供的json对象,用于将String对象、javaBean、Collection等解析为JSON格式的对象。
3) JSONArray: FastJson提供json数组对象。
Fastjson中的经常调用的方法
parse(String text);: 把JSON文本parse为JSONObject或者JSONArray
parseObject(String text);: 把JSON文本parse成JSONObject
parseArray(String text);: 把JSON文本parse成JSONArray
toJSONString(Object object);: 将JavaBean序列化为JSON文本
实例:
服务器接收请求数据,发送 JSON 回浏览器,并根据不同的请求方式,分别解决中文乱码问题:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/json;charset=utf-8");
//允许所有IP地址和端口请求 response.setHeader("Access-Control-Allow-Origin", "*");
//允许所有的文档类型请求 response.setHeader("Access-Control-Content-Type", "*");
HashMapmap = new HashMap();
map.put("user", new String(request.getParameter("user")
.getBytes("ISO-8859-1"), "utf-8"));
map.put("psw", new String(request.getParameter("psw")
.getBytes("ISO-8859-1"), "utf-8"));
System.out.println(new String(request.getParameter("user")
.getBytes("ISO-8859-1"), "utf-8"));
String jsonString = JSON.toJSONString(map);
response.getWriter()
.println(jsonString);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
request.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/json;charset=utf-8");
//允许所有IP地址和端口请求 response.setHeader("Access-Control-Allow-Origin", "*");
//允许所有的文档类型请求 response.setHeader("Access-Control-Content-Type", "*");
HashMapmap = new HashMap();
map.put("user", request.getParameter("user"));
map.put("psw", request.getParameter("psw"));
System.out.println(request.getParameter("user"));
String jsonString = JSON.toJSONString(map);
response.getWriter()
.println(jsonString);
}服务器接收 JSON 并取出数据发回浏览器:
浏览器:
$.ajax(
{
url: 'http://localhost:8080/RequestNResponse/GetJSON'
, method: 'GET'
, data:
{
json: `{ "username": "张三", "age": 23, "hobby":["篮球","足球","羽毛球"] }`
}
, complete: function (res)
{
console.log(res)
$('body')
.append(`${res.responseText}
`)
}
})服务器:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//允许所有IP地址和端口请求 response.setHeader("Access-Control-Allow-Origin", "*");
//允许所有的文档类型请求 response.setHeader("Access-Control-Content-Type", "*");
String jsonStr = new String(request.getParameter("json")
.getBytes("ISO-8859-1"), "utf-8");
JSONObject object = JSON.parseObject(jsonStr);
response.getWriter()
.println("username:" + object.get("username"));
response.getWriter()
.println("username:" + object.get("age"));
JSONArray hobbies = (JSONArray) object.get("hobby");
hobbies.forEach(obj - >
{
try
{
response.getWriter()
.println(obj);
}
catch (IOException e)
{
// TODO Auto-generated catch block e.printStackTrace();
}
});
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//允许所有IP地址和端口请求 response.setHeader("Access-Control-Allow-Origin", "*");
//允许所有的文档类型请求 response.setHeader("Access-Control-Content-Type", "*");
String jsonStr = request.getParameter("json");
JSONObject object = JSON.parseObject(jsonStr);
response.getWriter()
.println("username:" + object.get("username"));
response.getWriter()
.println("username:" + object.get("age"));
JSONArray hobbies = (JSONArray) object.get("hobby");
hobbies.forEach(obj - >
{
try
{
response.getWriter()
.println(obj);
}
catch (IOException e)
{
// TODO Auto-generated catch block e.printStackTrace();
}
});
}好了,关于fastjson的相关内容就分享到这里,fastjson数据处理速度快,功能完善,是大家开发的好帮手。最后大家如果想要了解更多json相关知识,敬请关注奇Q工具网。
推荐阅读: