JSON对象和JSON字符串以及JSON.parse 函数的使用

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JSON.parse()</title>
<script type="text/javascript">
	//示例1:此示例使用 JSON.parse 将 JSON 字符串转换为对象
	var jsontext = '{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}';//JSON 字符串
	var contact = JSON.parse(jsontext);
	document.write(contact.surname + ", " + contact.firstname + ", "+ contact.phone);

	//示例2:和实例1是一样的效果
	var jsontext2 = {"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]};//JSON 对象
	//var contact2 = JSON.parse(jsontext2);
	document.write("<br /><br />"+jsontext2.surname + ", " + jsontext2.firstname + ", "+ jsontext2.phone);
</script>
</head>
<body>
</body>
</html>

输出:

Aaberg, Jesper, 555-0100,555-0120

Aaberg, Jesper, 555-0100,555-0120


************************************************************************************

前端页面接收JSON对象的实例:

<script>	
	sendRecord('1');
	function sendRecord(record){
		var req = {
			user_id:<?php echo $userId;?>,
			record:record,
		}
		$.ajax({
			url: "./a.php",
			type:"post",
			data:req,
			dataType:"JSON", //返回数据格式为JSON对象
			success: function(res){
			    alert(Object.prototype.toString.apply(res));//alert输出[object Object],不用JSON.parse()解析
				if(res.result==1){
					alert('11');
				}else if(res.result==2){
					alert('22');
				}else if(res.result==3){
					alert('33');
				}
			},
			error: function(){
				alert('error000');
				console.log(this);
			}
		});
	}
</script>
a.php

<?php
	$record = $_POST['record'];
	
	if ($record==1) {                    
		$json['result'] = 1;                    
	}elseif($record==2){
		$json['result'] = 2;
	}elseif($record==3){
		$json['result'] = 3;
	}

	$json = json_encode($json);
	echo $json;  //{"result":1}
?>

***************************************************************************

<script type="text/javascript" src="./jquery.min.js"></script>
<script>	
	sendRecord('2');
	function sendRecord(record){
		var req = {
			record:record,
		}
		$.ajax({
			url: "./b.php",
			type:"post",
			data:req,
			dataType:"JSON", //返回数据格式为JSON对象
			success: function(res){
				//加上dataType:"JSON",alert输出[object Array],不需要JSON.parse()解析
				//不加dataType:"JSON",alert输出[object String],需要JSON.parse()解析
				alert(Object.prototype.toString.apply(res));
				//var res2 = JSON.parse(res);
				alert(res);
			},
			error: function(){
				alert('error000');
				console.log(this);
			}
		});
	}
</script>
b.php

<?php
	$record = $_POST['record'];
	if ($record==1) {                    
		$json = array('a','aa');                    
	}elseif($record==2){
		$json = array('b','bb'); 
	}elseif($record==3){
		$json = array('c','cc'); 
	}
	$json = json_encode($json);
	echo $json;  //["b","bb"]
?>

总结:如果后台是通过echo json_encode();输出json字符串的话,而且前台使用了dataType:"JSON", 那么则不需要JSON.parse();把json字符串转换成对象或数组。



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值