如何在Android中使用java将JSONObject结果存储到BeanClass中?
我是新手,很难将JSonObject保存到我的Beanclass *这是JSON对象:* 根据本LOC的结果:如何在Android中使用java将JSONObject结果存储到BeanClass中?,java,android,json,javabeans,jsonresult,Java,Android,Json,Javabeans,Jsonresult,我是新手,很难将JSonObject保存到我的Beanclass *这是JSON对象:* 根据本LOC的结果: JSONObject responseJson; Log.i("LoginActivity",responseJson.toString()); 11-28 22:15:56.525:I/LoginActivity(22793):{“消息”:“成功登录”,“授权”:“全球管理员,安装人员,香港,项目经理,SendPdfToCustomerRole,测量员”,“用户”:{“查看所有
JSONObject responseJson;
Log.i("LoginActivity",responseJson.toString());
11-28 22:15:56.525:I/LoginActivity(22793):{“消息”:“成功登录”,“授权”:“全球管理员,安装人员,香港,项目经理,SendPdfToCustomerRole,测量员”,“用户”:{“查看所有建议”:“0”,“角色id”:“1”,“联系人号码”:“020 8547 4333(x354)”,“修改日期”:“2013-10-10 10:08:07”,“状态”:“活动”,“利润率限制”:,“现金代码”:“KHO”,“创建日期”:“2013-02-03 06:41:41”,“用户姓名首字母”:“KHO”,“密码”:“f10343d1dc8d44c8935b356aa3f8aae2”,“id”:“27”,“名字”:“Kong”,“用户名”:“Kong”,“is_管理员”:“0”,“地址”:“技术经理”,“电子邮件”:“Kong”。hoang@mrfsgroup.com,“提案审查”:“3000000”,“姓氏”:“Hoang”,“ldap\U身份验证”:“0”},“状态”:trueapkStatus“:true,“apkFileDate”:“2013-11-25 05:07:27”,“apkFile”:“34_SurveyAppNew.apk”}
和我的Bean类:
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;
}
}
有人能告诉我如何将JSON响应保存到我的BeanClass中吗?您可以通过大量代码来完成。由于您是新手,我不建议您使用此库
Gson
,但它会解决您的问题。使用Gson
只需一行代码即可 使用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.
我想你正需要这个 仔细阅读,您将找到问题的解决方案:)
Happy Codddding:)是的,我已经在我的代码中使用了Gson,但我不知道如何将jsonResponse中的所有值插入BeanClass中。如何将jsonResponse中的所有值插入BeanClass中?我不明白。您将得到一个包含所有值的Loginbean对象。您可以调用getter来检索字段的值。@user1668447请参阅编辑。如果您有任何问题,请回答。您不必使用set添加任何值。语句
LoginBean gotFromJson=gson.fromJson(json,LoginBean.class)
将为您做一切。只要尝试上面的代码,您就会理解。这里的gotFromJson
是您的LoginBean
对象。让我们