一、Fastjson API入口类是com.alibaba.fastjson.JSON,常用的序列化方法:
//把JSON文本解析为JSONObject或者JSONArray
public
// 把JSON文本解析成JSONObject
public
// 把JSON文本解析为JavaBean
public
// 把JSON文本解析成JSONArray
public
//把JSON文本解析成JavaBean集合
public
// 将JavaBean序列化为JSON文本
public
// 将JavaBean序列化为带格式的JSON文本
public
//将JavaBean转换为JSONObject或者JSONArray。
public
二、FastJson解析JSON步骤

public static String createJsonString(Object value){
}
B、客户端将json字符串转换为相应的javaBean
首先客户端也要导入fastjson的两个jar包
1、客户端获取json字符串
public class HttpUtil {
public static String fetch(Request request, Map<String, String> params) throws IOException {
return fetch(request, params, "UTF-8");
}
public static String fetch(Request request, Map<String, String> params, String charset) throws IOException {
Form form = Form.form();
for (Map.Entry<String, String> entry : params.entrySet()) {
form.add(entry.getKey(), entry.getValue());
}
return fetch(request.bodyForm(form.build(), Charset.forName(charset)));
}
public static String fetch(Request request, Form bodyForm) throws IOException {
return fetch(request.bodyForm(bodyForm.build(), Charset.forName("UTF-8")));
}
public static String fetch(Request request, String bodyString) throws IOException {
return fetch(request.bodyString(bodyString, ContentType.create("text/plain", "UTF-8")));
}
public static String fetch(Request request) throws IOException {
Executor executor = Executor.newInstance();
String result = executor.execute(request).returnContent().asString();//字符串
executor.clearCookies();
return result;
}
public static InputStream fetchInputStream(Request request) throws IOException {
Executor executor = Executor.newInstance();
InputStream is = executor.execute(request).returnContent().asStream();//流
executor.clearCookies();
return is;
}
/**
* 获取Cookie方法
* @param request request对象
* @param name Cookie名称
* @return 值
*/
public static String getCookie(HttpServletRequest request, String name) {
Cookie[] cookies = request.getCookies();
if ( cookies != null ) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals(name)) {
return cookie.getValue();
}
}
}
return null;
}
/**
* 添加Cookie方法
* @param response response对象
* @param name Cookie名称
* @param value 值
*/
public static void addCookie(HttpServletResponse response, String name, String value) {
addCookie(response, name, value, 1 * 365 * 24 * 60 * 60);
}
/**
* 添加Cookie方法
* @param response response对象
* @param name Cookie名称
* @param value 值
* @param maxAge 最长存活时间(秒)
*/
public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) {
addCookie(response, name, value, maxAge, "/");
}
/**
* 添加Cookie方法
* @param response response对象
* @param name Cookie名称
* @param value 值
* @param maxAge 最长存活时间(秒)
* @param path 存放路径
*/
public static void addCookie(HttpServletResponse response, String name, String value, int maxAge, String path) {
Cookie cookie = new Cookie(name, value);
cookie.setMaxAge(maxAge);
cookie.setPath(path);
response.addCookie(cookie);
}
/**
* 删除Cookie方法
*/
public static void removeCookie(HttpServletResponse response, String name) {
addCookie(response, name, null, -1);
}
}
2、使用泛型获取javaBean
List<Person> pList = JSON.parseArray(jsonString, Person.class);
HttpUtil.fetch(Request.Post(url), JSONObject.toJSONString(person));//post方式提交
HttpUtil.fetch(Request.Get(url), JSONObject.toJSONString(person));//get方式提交