JSON 之FastJson遠端解析
一、阿里巴巴FastJson是一個Json處理工具包,包括“序列化”和“反序列化”兩部分,它具備如下特徵:
速度最快,測試表明,fastjson具有極快的效能,超越任其他的Java Json parser。包括自稱最快的JackJson;
功能強大,完全支援Java Bean、集合、Map、日期、Enum,支援範型,支援自省;無依賴,能夠直接執行在Java SE 5.0以上版本;支援Android;開源 (Apache 2.0)
二、FastJson解析JSON步驟
然後將資料轉為json字串,核心函式是:
public static String createJsonString(Object value)
{
String alibabaJson = JSON.toJSONString(value);
return alibabaJson;
}
B、客戶端將json字串轉換為相應的javaBean
首先客戶端也要匯入fastjson的兩個jar包
1、客戶端獲取json字串
public class HttpUtil
{
public static String getJsonContent(String urlStr)
{
try
{// 獲取HttpURLConnection連線物件
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url
.openConnection();
// 設定連線屬性
httpConn.setConnectTimeout(3000);
httpConn.setDoInput(true);
httpConn.setRequestMethod("GET");
// 獲取相應碼
int respCode = httpConn.getResponseCode();
if (respCode == 200)
{
return ConvertStream2Json(httpConn.getInputStream());
}
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
private static String ConvertStream2Json(InputStream inputStream)
{
String jsonStr = "";
// ByteArrayOutputStream相當於記憶體輸出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
// 將輸入流轉移到記憶體輸出流中
try
{
while ((len = inputStream.read(buffer, 0, buffer.length)) != -1)
{
out.write(buffer, 0, len);
}
// 將記憶體流轉換為字串
jsonStr = new String(out.toByteArray());
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonStr;
}
}
2、使用泛型獲取javaBean(核心函式)
public static T getPerson(String jsonString, Class cls) {
T t = null;
try {
t = JSON.parseObject(jsonString, cls);
} catch (Exception e) {
// TODO: handle exception
}
return t;
}
public static List getPersons(String jsonString, Class cls) {
List list = new ArrayList();
try {
list = JSON.parseArray(jsonString, cls);
} catch (Exception e) {
}
return list;
}
public static List > listKeyMaps(String jsonString) {
List > list = new ArrayList >();
try {
list = JSON.parseObject(jsonString,
new TypeReference>>() {
});
} catch (Exception e) {
// TODO: handle exception
}
return list;
}
出處:http://blog.sina.com.cn/s/blog_7ffb8dd501013qas.html
速度最快,測試表明,fastjson具有極快的效能,超越任其他的Java Json parser。包括自稱最快的JackJson;
功能強大,完全支援Java Bean、集合、Map、日期、Enum,支援範型,支援自省;無依賴,能夠直接執行在Java SE 5.0以上版本;支援Android;開源 (Apache 2.0)
Fastjson
API入口類是com.alibaba.fastjson.JSON,常用的序列化操作都可以在JSON類上的靜態方法直接完成。
public static final Object
parse(String text); // 把JSON文字parse為JSONObject或者JSONArray
public static final JSONObject parseObject(String text); // 把JSON文字parse成JSONObject
public static final T parseObject(String text, Class clazz); // 把JSON文字parse為JavaBean
public static final JSONArray parseArray(String text); // 把JSON文字parse成JSONArray
public static final List parseArray(String text, Class clazz); //把JSON文字parse成JavaBean集合
public static final String toJSONString(Object object); // 將JavaBean序列化為JSON文字
public static final String toJSONString(Object object, boolean prettyFormat); // 將JavaBean序列化為帶格式的JSON文字
public static final Object toJSON(Object javaObject); 將JavaBean轉換為JSONObject或者JSONArray。
public static final JSONObject parseObject(String text); // 把JSON文字parse成JSONObject
public static final T parseObject(String text, Class clazz); // 把JSON文字parse為JavaBean
public static final JSONArray parseArray(String text); // 把JSON文字parse成JSONArray
public static final List parseArray(String text, Class clazz); //把JSON文字parse成JavaBean集合
public static final String toJSONString(Object object); // 將JavaBean序列化為JSON文字
public static final String toJSONString(Object object, boolean prettyFormat); // 將JavaBean序列化為帶格式的JSON文字
public static final Object toJSON(Object javaObject); 將JavaBean轉換為JSONObject或者JSONArray。
二、FastJson解析JSON步驟
A、伺服器端將資料轉換成json字串
首先、伺服器端專案要匯入阿里巴巴的fastjson的jar包至builtPath路徑下(這些可以到fastjson官網下載:http://code.alibabatech.com/wiki/display/FastJSON/Home-zh)
首先、伺服器端專案要匯入阿里巴巴的fastjson的jar包至builtPath路徑下(這些可以到fastjson官網下載:http://code.alibabatech.com/wiki/display/FastJSON/Home-zh)
public static String createJsonString(Object value)
{
String alibabaJson = JSON.toJSONString(value);
return alibabaJson;
}
B、客戶端將json字串轉換為相應的javaBean
首先客戶端也要匯入fastjson的兩個jar包
1、客戶端獲取json字串
public class HttpUtil
{
public static String getJsonContent(String urlStr)
{
try
{// 獲取HttpURLConnection連線物件
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url
.openConnection();
// 設定連線屬性
httpConn.setConnectTimeout(3000);
httpConn.setDoInput(true);
httpConn.setRequestMethod("GET");
// 獲取相應碼
int respCode = httpConn.getResponseCode();
if (respCode == 200)
{
return ConvertStream2Json(httpConn.getInputStream());
}
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
private static String ConvertStream2Json(InputStream inputStream)
{
String jsonStr = "";
// ByteArrayOutputStream相當於記憶體輸出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
// 將輸入流轉移到記憶體輸出流中
try
{
while ((len = inputStream.read(buffer, 0, buffer.length)) != -1)
{
out.write(buffer, 0, len);
}
// 將記憶體流轉換為字串
jsonStr = new String(out.toByteArray());
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonStr;
}
}
2、使用泛型獲取javaBean(核心函式)
public static T getPerson(String jsonString, Class cls) {
T t = null;
try {
t = JSON.parseObject(jsonString, cls);
} catch (Exception e) {
// TODO: handle exception
}
return t;
}
public static List getPersons(String jsonString, Class cls) {
List list = new ArrayList();
try {
list = JSON.parseArray(jsonString, cls);
} catch (Exception e) {
}
return list;
}
public static List > listKeyMaps(String jsonString) {
List > list = new ArrayList >();
try {
list = JSON.parseObject(jsonString,
new TypeReference>>() {
});
} catch (Exception e) {
// TODO: handle exception
}
return list;
}
出處:http://blog.sina.com.cn/s/blog_7ffb8dd501013qas.html
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29119536/viewspace-1690345/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- JSON解析器之Gson、FastJson、JacksonJSONAST
- FastJSON解析Json字串(反序列化為List、Map)ASTJSON字串
- fastjson: json物件,json物件陣列,javabean物件,json字串之間的相互轉化ASTJSON物件陣列JavaBean字串
- JSON學習--com.alibaba.fastjson.JSONJSONAST
- 隨記(九):記錄Fastjson遠端命令執行流程ASTJSON
- Flutter開發之JSON解析FlutterJSON
- fastjson:物件轉化成json出現$refASTJSON物件
- fastjson字串轉JSON的$ref問題ASTJSON字串
- alibaba/fastjson 之 JSONPathASTJSON
- Java中使用Fastjson將JSON檔案轉物件JavaASTJSON物件
- java通過url呼叫遠端介面返回json資料JavaJSON
- 使用python遠端操作linux過程解析PythonLinux
- fastjson判斷JSON字串是Object還是List<Object>ASTJSON字串Object
- 關於fastjson出現反序列化遠端程式碼執行漏洞的通知ASTJSON
- Jackson 解析json資料之忽略解析欄位註解@JsonIgnorePropertiesJSON
- debug技巧之遠端除錯除錯
- Dapr 遠端除錯之 Nocalhost除錯
- java安全之fastjson鏈分析JavaASTJSON
- Exercise:JSON解析JSON
- cJSON:解析JSONJSON
- js json解析JSON
- Fastjson 1.2.24遠端程式碼執行漏洞(com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl)ASTJSONApache
- 阿里雲之遠端連線mysql阿里MySql
- 使用ajax請求傳送複雜的json資料型別,並解決fastjson解析複雜的json資料型別的問題JSON資料型別AST
- 高危!Fastjson反序列化遠端程式碼執行漏洞風險通告,請儘快升級ASTJSON
- Fastjson反序列化遠端程式碼執行漏洞產生原因及修復建議ASTJSON
- Java安全之Fastjson內網利用JavaASTJSON內網
- Java安全之FastJson JdbcRowSetImpl 鏈分析JavaASTJSONJDBC
- dubbo原始碼解析(三十)遠端呼叫——rest協議原始碼REST協議
- Golang 流式解析 JsonGolangJSON
- 用JS解析JSONJSON
- java解析json listJavaJSON
- Swift iOS : 解析jsonSwiftiOSJSON
- json解析模組JSON
- git操作之pull拉取遠端指定分支以及push推送到遠端指定分支Git
- dubbo原始碼解析(三十一)遠端呼叫——rmi協議原始碼協議
- dubbo原始碼解析(三十二)遠端呼叫——thrift協議原始碼協議
- Flutter 中的 JSON 解析FlutterJSON
- oracle json 解析函式OracleJSON函式