JSON对象和JSON字符串

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <title>JSON.parse()</title>  
  5. <script type="text/javascript">  
  6.     //示例1:此示例使用 JSON.parse 将 JSON 字符串转换为对象  
  7.     var jsontext = '{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}';//JSON 字符串  
  8.     var contact = JSON.parse(jsontext);  
  9.     document.write(contact.surname + ", " + contact.firstname + ", "+ contact.phone);  
  10.   
  11.     //示例2:和实例1是一样的效果  
  12.     var jsontext2 = {"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]};//JSON 对象  
  13.     //var contact2 = JSON.parse(jsontext2);  
  14.     document.write("<br /><br />"+jsontext2.surname + ", " + jsontext2.firstname + ", "+ jsontext2.phone);  
  15. </script>  
  16. </head>  
  17. <body>  
  18. </body>  
  19. </html>  

输出:

Aaberg, Jesper, 555-0100,555-0120

Aaberg, Jesper, 555-0100,555-0120


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

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

[javascript]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <script>    
  2.     sendRecord('1');  
  3.     function sendRecord(record){  
  4.         var req = {  
  5.             user_id:<?php echo $userId;?>,  
  6.             record:record,  
  7.         }  
  8.         $.ajax({  
  9.             url: "/3G/wall/ajax_send_record/",  
  10.             type:"post",  
  11.             data:req,  
  12.             dataType:"JSON"//返回数据格式为JSON对象  
  13.             success: function(res){  
  14.                 if(res.result==1){  //因为传递过来是JSON对象,所以不用<span style="font-family:Simsun;">JSON.parse()解析</span>  
  15.                     alert('11');  
  16.                 }else if(res.result==2){  
  17.                     alert('22');  
  18.                 }else if(res.result==3){  
  19.                     alert('33');  
  20.                 }  
  21.             },  
  22.             error: function(){  
  23.                 alert('error000');  
  24.                 console.log(this);  
  25.             }  
  26.         });  
  27.     }  
  28. </script>  


[php]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2.     function ajax_send_record()  
  3.     {  
  4.         $record = $_POST('record');  
  5.           
  6.         if ($record==1) {                      
  7.             $json['result'] = 1;                      
  8.         }elseif($record==2){  
  9.             $json['result'] = 2;  
  10.         }elseif(elseif($record==3){  
  11.             $json['result'] = 3;  
  12.         }  
  13.   
  14.         $json = json_encode($json);  
  15.         echo $json;  
  16.     }  
  17. ?>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值