阿里巴巴FastJson解析

本文详细介绍了Fastjson API的使用方法,包括如何将数据转化为JSON字符串及从JSON字符串中解析出数据,同时提供了JSON数据操作的步骤与实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Fastjson API入口类是com.alibaba.fastjson.JSON,常用的序列化方法:

//把JSON文本解析为JSONObject或者JSONArray          

public static final Object parse(String text); 

// 把JSON文本解析成JSONObject 
public static final JSONObject parseObject(String text);

// 把JSON文本解析为JavaBean    
public static final  T parseObject(String text, Class clazz); 

// 把JSON文本解析成JSONArray 
public static final JSONArray parseArray(String text); 

//把JSON文本解析成JavaBean集合 
public static final  List parseArray(String text, Class clazz); 

// 将JavaBean序列化为JSON文本 
public static final String toJSONString(Object object); 

// 将JavaBean序列化为带格式的JSON文本 
public static final String toJSONString(Object object, boolean prettyFormat); 

//将JavaBean转换为JSONObject或者JSONArray。
public static final Object toJSON(Object javaObject); 

二、FastJson解析JSON步骤
 

A、服务器端将数据转换成json字符串
     首先导入jar包至builtPath路径下(这些可以到fastjson官网下载:http://code.alibabatech.com/wiki/display/FastJSON/Home-zh
JSON <wbr>之FastJson解析 然后将数据转为json字符串,核心函数是:
public static String createJsonString(Object value){
   String str = JSON.toJSONString(value);
   return str;
}

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    
    Person p = JSON.parseObject(jsonString,Person.class);

List<Person> pList = JSON.parseArray(jsonString, Person.class);
    

3、提交方式

HttpUtil.fetch(Request.Post(url), JSONObject.toJSONString(person));//post方式提交

HttpUtil.fetch(Request.Get(url), JSONObject.toJSONString(person));//get方式提交

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值