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

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"));


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值