jquery将json转为数据字典

9 篇文章 0 订阅

1.基础——定义json文件

{
	"userinfo": [
			{ "account":"15639864589" , "password":"123456789" , "email":"123456789@qq.com" },
			{ "account":"15652689689" , "password":"123456789" , "email":"123456789@qq.com" },
			{ "account":"15635456989" , "password":"123456789" , "email":"123456789@qq.com" },
			{ "account":"15632145689" , "password":"123456789" , "email":"123456789@qq.com" }
		]
}

目录如下:

2.ajax请求json jquery转换为数据字典

我们通过ajax请求该json文件的数据  将其转换为字典

//-----基础信息start
//用户信息字典
var userinfoDic = {};
$.ajax({
	//请求方式为get
	type:"GET",
	//json文件位置
    url:"./json/user.json",
	//返回数据格式为json
	dataType: "json",
	//请求成功完成后要执行的方法
	success: function(data){
		//使用$.each方法遍历返回的数据date
		$.each(data.userinfo,function(i,item){
			//设置根据用户账号密码找用户
			userinfoDic[item.account,item.password] = item;
		});
	}
});
//-----基础信息end

3.字典测试

//登录按钮的点击事件
$("#login").bind('click',function(){
	var account = $("#account").val();
	var password = $("#password").val();
	//
	if(account != '' && password != ''){
	    if(userinfoDic[account,password]){
		    window.location.href = 'index.html';
	    }else{
		    alert("账号或密码错误!");
	    }
	}else{
		alert("账号或密码不能为空!");
	}
					
});

4.测试截图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值