1.实体类(以实体类为基础,可以不使用)
public class Users {
String userName;
String userAccount;
String userID;
String userPwd;
public Users() {
super();
}
public Users(String userName, String userAccount, String userID, String userPwd) {
super();
this.userName = userName;
this.userAccount = userAccount;
this.userID = userID;
this.userPwd = userPwd;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserAccount() {
return userAccount;
}
public void setUserAccount(String userAccount) {
this.userAccount = userAccount;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
@Override
public String toString() {
return "Users [userName=" + userName + ", userAccount=" + userAccount + ", userID=" + userID + ", userPwd="
+ userPwd + "]";
}
}
2.存
//一个实体 JSONObject studentJSONObject = new JSONObject(); studentJSONObject.put("userid","value-id"); studentJSONObject.put("username","value-name"); studentJSONObject.put("useraccount","value-account); studentJSONObject.put("userpwd","value-pwd");
//或者 JSONObject studentJSONObject = new JSONObject(); Users user = new Users("1","2","3","4"); studentJSONObject = new JSONObject(user);
JSONObject studentJSONObject = new JSONObject();//多个实体 studentJSONObject.put("userid","value-id"); studentJSONObject.put("username","value-name"); studentJSONObject.put("useraccount","value-account); studentJSONObject.put("userpwd","value-pwd"); JSONArray array = new JSONArray(); array.put(studentJSONObject);
3.取//单个实体 Users user = new Users(); JSONObject studentJSONObject = new JSONObject(JSONText); user.setUserID(studentJSONObject.getString("userid")) ; user.setUserName(studentJSONObject.getString("username")); user.setUserAccount(studentJSONObject.getString("useraccount")); user.setUserPwd(studentJSONObject.getString("userpwd"));
//多个实体 Users user = new Users(); JSONArray array = new JSONArray(JSONText); JSONObject studentJSONObject = array.getJSONObject(0); user.setUserID(studentJSONObject.getString("userid")) ; user.setUserName(studentJSONObject.getString("username")); user.setUserAccount(studentJSONObject.getString("useraccount")); user.setUserPwd(studentJSONObject.getString("userpwd"));