1例如:

List-->json:

Java代码
  1. Listlist=newArrayList();

  2. for(inti=0;i

  3. list.add(i);

  4. }

  5. JSONArrayjsonArray=JSONArray.fromObject(stus);

  6. System.out.println(jsonArray);

List list = new ArrayList(); 
for (int i = 0; i < stus.length; i++) { 
list.add(i);
} 
JSONArray jsonArray = JSONArray.fromObject(stus);
System.out.println(jsonArray); 

2例如:


对象(Student)-->json:


Java代码
  1. Studentstudent=newStudent();

  2. student.setAge(18);

  3. student.setName("zhangsan");

  4. JSONObjectjsonObject=JSONObject.fromObject(student);

  5. System.out.println(jsonObject);

Student student = new Student();
student.setAge(18);
student.setName("zhangsan");
JSONObject jsonObject = JSONObject.fromObject(student);
System.out.println(jsonObject); 

3综合

List-->json


Java代码
  1. Student[]stus=newStudent[5];

  2. ListstuList=newArrayList();

  3. for(inti=0;i

  4. stus[i]=newStudent();

  5. stus[i].setAge(20);

  6. stus[i].setName("张三"+i);

  7. stuList.add(stus[i]);

  8. }

  9. JSONObjectjsonObjectFromMap=JSONObject.fromObject(stuList);

  10. System.out.println(jsonObjectFromMap);

Student[] stus = new Student[5]; 
List stuList = new ArrayList(); 
for (int i = 0; i < stus.length; i++) { 
stus[i] = new Student();
stus[i].setAge(20); 
stus[i].setName("张三" + i);
stuList.add(stus[i]);
}
JSONObject jsonObjectFromMap = JSONObject.fromObject(stuList);
System.out.println(jsonObjectFromMap);  



举例:从数据库中查询出一组数据,用ajax传到页面上,并解析生成下拉列表项



Action中:

//获得传过来的机构ID
Stringorg_id=request.getParameter("org_id");

Connectioncon=ConnectionFactory.getInstance().getConnection();
Statementpst=null;
ResultSetrs=null;
Stringsql="";

sql="selectc.group_ID,c.group_namefromgroupinfocwherec.group_ID!='"
+org_id
+"'startwithc.group_ID='"
+org_id
+"'Connectbypriorc.group_ID=c.parent_ID";


pst=con.createStatement();
rs=pst.executeQuery(sql);
JSONArrayarr=newJSONArray();
//遍历
while(rs.next()){
JSONObjectjson=newJSONObject();
json.put("GroupId",rs.getString("group_ID"));
json.put("GroupName",rs.getString("group_name"));
arr.add(json);
}
PrintWriterout=response.getWriter();
out.print(arr.toString());
returnnull;


页面中的获取传过来的list,并进行遍历,生成下拉列表项

js中:

functionchange(obj){
varselectValue=document.getElementById('org_id').options[document.getElementById('org_id').selectedIndex].text;
document.getElementById("selectValue").value=selectValue;
//window.location="<%//=request.getContextPath()%>//statisticsAction.do?cmd=toMmsStaticsByGroup&groupId="+document.getElementById('org_id').value;
varurl='<%=request.getContextPath()%>/statisticsAction.do';
varpars='cmd=getGroupByOrg&org_id='+document.getElementById('org_id').value;
varmyAjax=newAjax.Request(url,{method:'post',parameters:pars,onComplete:_complete});
}
//请求完成的处理
function_complete(originalRequest)
{
varstatusValue=originalRequest.status;
vararr=originalRequest.responseText;
if(statusValue==200)
{
vargroup=document.getElementById("columnInfoId");
group.length=0;
group.options.add(newOption("--全部--","ALL"));
varjsonss=eval("("+arr+")");
for(vari=0;igroup.options.add(newOption(jsonss[i].GroupName,jsonss[i].GroupId));
}
}
elseif(statusValue==404)
{
alert("代码出现错误,链接地址有误");
}
else
{
alert("代码出现未知错误");

}
}

页面布局中:





机    构:




${org['GroupName']}


${org['GroupName']}






群    组:



---全部---






必须jar包:http://down.51cto.com/data/1009559