您的位置:首页 > 编程语言 > Java开发

org.json简单使用方法(Java)

2017-06-09 12:43 597 查看
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"));



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息