如何使用Java在Android中的JSONObject结果存储到BeanClass? [英] how to store JSONObject result into BeanClass using java In Android?
问题描述
我是新手,越来越困难,我的JSONObject保存到我的 Beanclass
*的这是JSON对象:的*
在这个LOC的结果是:
的JSONObject responseJson;
Log.i(LoginActivity,responseJson.toString());
22 11-28:15:56.525:I / LoginActivity(22793):{消息:成功登录。,AUTHS:全局管理员,安装,孔,项目经理,SendPdfToCustomerRole,验船师 用户:{view_all_proposals:0,ROLE_ID:1,contact_no:020 8547 4333(x354),DATE_MODIFIED:2013年10月10日10时08分07秒,状态:ACTIVE,profit_margin_limit:,cash_ code:KHO,DATE_CREATED:2013年2月3日06:41:41\",\"user_initials\":\"KHO\",\"password\":\"f10343d1dc8d44c8935b356aa3f8aae2\",\"id\":\"27\",\"first_name\":\"Kong\",\"username\":\"kong\",\"is_admin\":\"0\",\"address\":\"Technology Manager\",\"email\":\"kong.hoang@mrfsgroup.com\",\"proposal_review\":\"3000000\",\"last_name\":\"Hoang\",\"ldap_authentication\":\"0\"},\"status\":true,\"apkStatus\":true,\"apkFileDate\":\"2013-11-25 5时07分27秒,apkFile:34_SurveyAppNew.apk}
块引用>和我Bean类:
公共类LoginBean {
私人诠释身份证;
私人字符串用户名;
私人字符串密码;
私人字符串电子邮件;
私人字符串状态;
私人日期ACTIVE;
dateCreated会私人日期;
私人日期dateModified;
私人字符串isAdmin; //为0
私人字符串USERINITIALS;
私人诠释角色ID;
私有String的firstName;
私人字符串的lastName;
私人字符串contactNo;
私人字符串的地址;
私人字符串profitMarginLimit;/ **
返回:标识
* /
公众诠释的getId(){
返回ID;
}
/ **
* @参数ID与id设置
* /
公共无效SETID(INT ID){
this.id = ID;
}
/ **
返回:用户名
* /
公共字符串getUsername(){
返回用户名;
}
/ **
* @参数用户名用户名设置
* /
公共无效setUsername(字符串username){
this.username =用户名;
}
/ **
返回:密码
* /
公共字符串getPassword来(){
返回密码;
}
/ **
*参数密码密码设置
* /
公共无效setPassword(字符串密码){
this.password =密码;
}
/ **
返回:电子邮件
* /
公共字符串getEmail(){
返回的电子邮件;
}
/ **
* @参数发送电子邮件的电子邮件设置
* /
公共无效setEmail(字符串email){
this.email =电子邮件;
}
/ **
返回:状态
* /
公共字符串的getStatus(){
返回状态;
}
/ **
* @参数状态的状态设置
* /
公共无效setStatus(字符串状态){
this.status =状态;
}
/ **
返回:活跃
* /
公开日期getACTIVE(){
回到ACTIVE状态;
}
/ **
* @参数活跃启动可
* /
公共无效SETACTIVE(日期ACTIVE){
ACTIVE =活跃;
}
/ **
*返回:dateCreated会
* /
公开日期getDateCreated(){
返回dateCreated会;
}
/ **
* @参数dateCreated会的dateCreated会设置
* /
公共无效setDateCreated(日期dateCreated会){
this.dateCreated = dateCreated会;
}
/ **
* @返回dateModified
* /
公开日期getDateModified(){
返回dateModified;
}
/ **
* @参数dateModified的dateModified设置
* /
公共无效setDateModified(日期dateModified){
this.dateModified = dateModified;
}
/ **
* @返回isAdmin
* /
公共字符串getIsAdmin(){
返回isAdmin;
}
/ **
* @参数isAdmin的isAdmin设置
* /
公共无效setIsAdmin(字符串isAdmin){
this.isAdmin = isAdmin;
}
/ **
* @返回USERINITIALS
* /
公共字符串getUserInitials(){
返回USERINITIALS;
}
/ **
* @参数USERINITIALS的USERINITIALS设置
* /
公共无效setUserInitials(字符串USERINITIALS){
this.userInitials = USERINITIALS;
}
/ **
返回:该角色ID
* /
公众诠释getRoleId(){
返回角色ID;
}
/ **
* @参数的角色ID角色ID设置
* /
公共无效setRoleId(INT角色ID){
this.roleId =角色ID;
}
/ **
* @返回的firstName
* /
公共字符串的getFirstName(){
返回的firstName;
}
/ **
* @参数名字中的firstName设置
* /
公共无效setFirstName(字符串的firstName){
this.firstName =名字;
}
/ **
返回:姓氏
* /
公共字符串getLastName(){
返回lastName的;
}
/ **
* @参数姓氏姓氏设置
* /
公共无效setLastName(字符串的lastName){
this.lastName = lastName的;
}
/ **
* @返回contactNo
* /
公共字符串getContactNo(){
返回contactNo;
}
/ **
* @参数contactNo的contactNo设置
* /
公共无效setContactNo(字符串contactNo){
this.contactNo = contactNo;
}
/ **
返回:地址
* /
公共字符串的getAddress(){
退货地址;
}
/ **
* @参数的地址,其设置
* /
公共无效setAddress(字符串地址){
this.address =地址;
}
/ **
* @返回profitMarginLimit
* /
公共字符串getProfitMarginLimit(){
返回profitMarginLimit;
}
/ **
* @参数profitMarginLimit的profitMarginLimit设置
* /
公共无效setProfitMarginLimit(字符串profitMarginLimit){
this.profitMarginLimit = profitMarginLimit;
}
}有人可以请指导我如何能节省JSON响应到我的 BeanClass
解决方案您可以通过大量的code线的做到这一点。既然你是一个新手,我不建议你使用这个库
GSON
但它会解决你的问题。使用code的它只是一条线GSON
使用GSON ...
GSON GSON =新GSON();
Loginbean豆=新的LoginBean(......); //你的构造
bean.setXYZ(....); // 如果你想
JSON字符串= gson.toJson(豆);
//而
LoginBean gotFromJson = gson.fromJson(JSON,LoginBean.class);
//如果JSON是从响应得到了JSON
//你可以在这里得到你的域的所有值。喜欢...
INT ID = gotFromJson.getId();
//等干将。它仍然会返回的值。i am newbie and getting difficulty to save my JSonObject into my Beanclass *this is JSON Object:* on result of this LOC:
JSONObject responseJson; Log.i("LoginActivity",responseJson.toString());
11-28 22:15:56.525: I/LoginActivity(22793): {"message":"Successfully Logged In.","auths":"Global Administrator,Installer,KONG,Project Manager,SendPdfToCustomerRole,Surveyor ","user":{"view_all_proposals":"0","role_id":"1","contact_no":"020 8547 4333 (x354)","date_modified":"2013-10-10 10:08:07","status":"ACTIVE","profit_margin_limit":"","cash_code":"KHO","date_created":"2013-02-03 06:41:41","user_initials":"KHO","password":"f10343d1dc8d44c8935b356aa3f8aae2","id":"27","first_name":"Kong","username":"kong","is_admin":"0","address":"Technology Manager","email":"kong.hoang@mrfsgroup.com","proposal_review":"3000000","last_name":"Hoang","ldap_authentication":"0"},"status":true,"apkStatus":true,"apkFileDate":"2013-11-25 05:07:27","apkFile":"34_SurveyAppNew.apk"}
and My Bean Class:
public class LoginBean { private int id; private String username; private String password; private String email; private String status; private Date ACTIVE; private Date dateCreated; private Date dateModified; private String isAdmin; // is '0' private String userInitials; private int roleId; private String firstName; private String lastName; private String contactNo; private String address; private String profitMarginLimit; /** * @return the id */ public int getId() { return id; } /** * @param id the id to set */ public void setId(int id) { this.id = id; } /** * @return the username */ public String getUsername() { return username; } /** * @param username the username to set */ public void setUsername(String username) { this.username = username; } /** * @return the password */ public String getPassword() { return password; } /** * @param password the password to set */ public void setPassword(String password) { this.password = password; } /** * @return the email */ public String getEmail() { return email; } /** * @param email the email to set */ public void setEmail(String email) { this.email = email; } /** * @return the status */ public String getStatus() { return status; } /** * @param status the status to set */ public void setStatus(String status) { this.status = status; } /** * @return the aCTIVE */ public Date getACTIVE() { return ACTIVE; } /** * @param aCTIVE the aCTIVE to set */ public void setACTIVE(Date aCTIVE) { ACTIVE = aCTIVE; } /** * @return the dateCreated */ public Date getDateCreated() { return dateCreated; } /** * @param dateCreated the dateCreated to set */ public void setDateCreated(Date dateCreated) { this.dateCreated = dateCreated; } /** * @return the dateModified */ public Date getDateModified() { return dateModified; } /** * @param dateModified the dateModified to set */ public void setDateModified(Date dateModified) { this.dateModified = dateModified; } /** * @return the isAdmin */ public String getIsAdmin() { return isAdmin; } /** * @param isAdmin the isAdmin to set */ public void setIsAdmin(String isAdmin) { this.isAdmin = isAdmin; } /** * @return the userInitials */ public String getUserInitials() { return userInitials; } /** * @param userInitials the userInitials to set */ public void setUserInitials(String userInitials) { this.userInitials = userInitials; } /** * @return the roleId */ public int getRoleId() { return roleId; } /** * @param roleId the roleId to set */ public void setRoleId(int roleId) { this.roleId = roleId; } /** * @return the firstName */ public String getFirstName() { return firstName; } /** * @param firstName the firstName to set */ public void setFirstName(String firstName) { this.firstName = firstName; } /** * @return the lastName */ public String getLastName() { return lastName; } /** * @param lastName the lastName to set */ public void setLastName(String lastName) { this.lastName = lastName; } /** * @return the contactNo */ public String getContactNo() { return contactNo; } /** * @param contactNo the contactNo to set */ public void setContactNo(String contactNo) { this.contactNo = contactNo; } /** * @return the address */ public String getAddress() { return address; } /** * @param address the address to set */ public void setAddress(String address) { this.address = address; } /** * @return the profitMarginLimit */ public String getProfitMarginLimit() { return profitMarginLimit; } /** * @param profitMarginLimit the profitMarginLimit to set */ public void setProfitMarginLimit(String profitMarginLimit) { this.profitMarginLimit = profitMarginLimit; } }
Can someone please guide me how can i save JSON response into my BeanClass.
解决方案You can do it by a lots of line of code. Since you are a newbie I don't recommend you to use this library
Gson
but it will solve your problem. Its just one line of code usingGson
Using Gson...
Gson gson = new Gson(); Loginbean bean = new LoginBean(......); // Your constructor bean.setXYZ(....); // If you want String json = gson.toJson(bean); // And LoginBean gotFromJson = gson.fromJson(json, LoginBean.class); // Where json is the JSON you got from response // You can get all the values of your fields here. like... int id = gotFromJson.getId(); // and other getters. it will still return the values.
这篇关于如何使用Java在Android中的JSONObject结果存储到BeanClass?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!