Parse Json data tools using fastjson framework

Source: Internet
Author: User

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;
}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.