JSONObject jsonObject = JSONObject.fromObject(productMap);
- Js代码
- var map =
- {
- 地名: ["北京","天津","上海"],
- 民族: ["汉族","藏族","维吾尔族"]
- };
- $.each(map,function(key,values){
- console.log(key);
- $(values).each(function(){
- console.log("/t" + this);
- });
- });
- var map =
- {
- 地名: ["北京","天津","上海"],
- 民族: ["汉族","藏族","维吾尔族"]
- };
- $.each(map,function(key,values){
- console.log(key);
- $(values).each(function(){
- console.log("/t" + this);
- });
- });
- 如果安装了firebug,控制台输出如下树状结构:
- 地名
- 北京
- 天津
- 上海
- 民族
- 汉族
- 藏族
- 维吾尔族
- 注意:jquery中的$().each和$.each的区别,前者只能遍历数组,后者可以遍历数组和对象
<%@ page language="java" import="java.util.*,net.sf.json.JSONObject" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
Map map = new HashMap();
map.put(1, "11");
map.put(2, "22");
map.put(3, "33");
JSONObject jsonObject = JSONObject.fromObject(map);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#hh").click(function(){
//发起异步请求,请求Action
//{"name":"xxx","age":xx,"o":{"oid":xx,"phone":"sss","address":"xxx"}}
$.post("/ajax_struts/abc/order.action"
,"name=zzzz"
,function(abc){
alert(abc.ois[0].productName);
alert(abc.orderId);
alert(abc);
//alert("phone is:"+abc.ois[0].productName+" age:"+abc.age);
}
,"json");
});
var map =
{
地名: ["北京","天津","上海"],
民族: ["汉族","藏族","维吾尔族"]
};
$.each(<%=jsonObject%>,function(key,values){
console.log(key);
// $(values).each(function(){
console.log("/t" + this);
// });
});
});
</script>
</head>
<body>
</body>
</html>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
Map map = new HashMap();
map.put(1, "11");
map.put(2, "22");
map.put(3, "33");
JSONObject jsonObject = JSONObject.fromObject(map);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#hh").click(function(){
//发起异步请求,请求Action
//{"name":"xxx","age":xx,"o":{"oid":xx,"phone":"sss","address":"xxx"}}
$.post("/ajax_struts/abc/order.action"
,"name=zzzz"
,function(abc){
alert(abc.ois[0].productName);
alert(abc.orderId);
alert(abc);
//alert("phone is:"+abc.ois[0].productName+" age:"+abc.age);
}
,"json");
});
var map =
{
地名: ["北京","天津","上海"],
民族: ["汉族","藏族","维吾尔族"]
};
$.each(<%=jsonObject%>,function(key,values){
console.log(key);
// $(values).each(function(){
console.log("/t" + this);
// });
});
});
</script>
</head>
<body>
</body>
</html>
JSONObject的toBean
Map map_ = (HashMap)JSONObject.toBean(jsonObject,HashMap.class);