加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

parseJSON 把string转化成object

发布时间:2020-12-16 19:28:03 所属栏目:百科 来源:网络整理
导读:jquery的ajax返回的如果是json格式,在success方法获取的值并不能直接当做json object处理,需要用$.parseJSON来转化成个json对象 例如 后台程序 ?phpinclude ("json_helper.php");$Josn = new Services_JSON ();$r = Array ("status"=1,"list"= array(Array

jquery的ajax返回的如果是json格式,在success方法获取的值并不能直接当做json object处理,需要用$.parseJSON来转化成个json对象

例如

后台程序

<?php
include ("json_helper.php");
$Josn = new Services_JSON ();
$r = Array ("status"=>1,"list"=> array(
		Array ("c_id" => 1,"c_parent_id" => 0,"c_name" => "旅游美食","c_status" => 0,"c_type" => 0,"c_area" => "反选:福建|湖南|内蒙古|" ),Array ("c_id" => 2,Array ("c_id" => 3,Array ("c_id" => 4,)
);
$r_josn = $Josn->encode($r); 
echo $r_josn; 

静态页面
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(function(){ 
	$("#getjson").bind("click",function(){
		$.ajax({
			  type: "POST",url: "/json/index.php",success: function(msg){ 
				   var r = $.parseJSON(msg); 
				   var tb = $("<table border=1></table>");
				   for(i=0;i<r.list.length;i++){
					   tb.append("<tr><td>"+r.list[i].c_id+"</td><td>"+r.list[i].c_name+"</td></tr>");
				   }
				   $("body").append(tb);
			   }

		});
	});
})
</script>
</head>
<body>
<button id="getjson">获取</button>
</body>
</html>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读