Android transforms an entity class class into a JSON string

Source: Internet
Author: User
Tags tojson

/**
* Convert entity classes to JSON string objects Note that this method requires a third-party Gson jar Package
* @param obj Object
* @return Map
*/
Private String ToJson (Object Obj,int method) {
TODO auto-generated Method Stub
if (method==1) {

field is the first letter lowercase, the remaining words are capitalized
Gson Gson = new Gson ();
String obj2 = Gson.tojson (obj);
return obj2;
}else if (method==2) {

Fieldnamingpolicy.lower_case_with_dashes all converted to lowercase, separated by spaces or underscores

Fieldnamingpolicy.upper_camel_case so capitalize the first letter of the word
Gson gson2=new Gsonbuilder (). Setfieldnamingpolicy (Fieldnamingpolicy.upper_camel_case). Create ();
String Obj2=gson2.tojson (obj);
return obj2;
}
Return "";
}

Android transforms an entity class class into a JSON string

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.