Parse Json data tools using fastjson framework
In Android Application Development, the data that the APP interacts with the server is usually in json format. To facilitate operations, we generally use the json parsing framework to help us perform data operations. Common frameworks include Gjson and fastjson. Fastjson is used in the project to parse the data. Of course, the premise is to prepare the corresponding jar package tool to convert the tool class (mainly the conversion operation between the json data and the object bean) post it for future use:
Public class FastJsonTools {
/**
* Use fastjson to parse the json string into a JavaBean
*
* @ Param jsonString
* @ Param cls
* @ Return
*/
Public static T getJson (String jsonString, Class Cls ){
T t = null;
Try {
T = JSON. parseObject (jsonString, cls );
} Catch (Exception e ){
// TODO: handle exception
E. printStackTrace ();
}
Return t;
}
/**
* Use fastjson to parse the json string into a List And List
*
* @ Param jsonString
* @ Param cls
* @ Return
*/
Public static List GetArrayJson (String jsonString, Class Cls ){
List List = new ArrayList ();
Try {
List = JSON. parseArray (jsonString, cls );
} Catch (Exception e ){
// TODO: handle exception
}
Return list;
}
/**
* Use fastjson to parse the json string into a List And List
*
* @ Param jsonString
* @ Param cls
* @ Return
*/
@ SuppressWarnings ("unchecked ")
Public static List GetArrayJson (String jsonString ){
List List = new ArrayList ();
Try {
List = (List ) JSON. parseArray (jsonString );
} Catch (Exception e ){
// TODO: handle exception
}
Return list;
}
/**
* Use fastjson to parse jsonString into List >
*
* @ Param jsonString
* @ Return
*/
Public static List > GetListMap (String jsonString ){
List > List = new ArrayList > ();
Try {
// Two writing methods
// List = JSON. parseObject (jsonString, new
// TypeReference >>() {}. GetType ());
List = JSON. parseObject (jsonString, new TypeReference >> (){
});
} Catch (Exception e ){
// TODO: handle exception
}
Return list;
}
}