1例如:

List-->json:

Java代码
  1. List<Student>list=newArrayList<Student>();

  2. for(inti=0;i<stus.length;i++){

  3. list.add(i);

  4. }

  5. JSONArrayjsonArray=JSONArray.fromObject(stus);

  6. System.out.println(jsonArray);

List<Student> list = new ArrayList<Student>(); 
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<Student>-->json


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

  2. List<Student>stuList=newArrayList<Student>();

  3. for(inti=0;i<stus.length;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<Student> stuList = new ArrayList<Student>(); 
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;i<jsonss.length;i++){
group.options.add(newOption(jsonss[i].GroupName,jsonss[i].GroupId));
}
}
elseif(statusValue==404)
{
alert("代码出现错误,链接地址有误");
}
else
{
alert("代码出现未知错误");

}
}

页面布局中:

<TABLEwidth="95%"align=centercellpadding="0"cellspacing="0">
<TBODY>

<TR>
<TDclass="zi_3F6293"align="left"height="35"width="50">机&nbsp;&nbsp;&nbsp;&nbsp;构:</TD>
<td>
<SELECTstyle="width:150px"name="groupId"id="org_id"value="${param.org_id}"onChange="change(this.options[this.options.selectedIndex].value)">
<c:forEachitems="${organizations}"var="org">
<c:iftest="${groupId==org['GroupId']}">
<OPTIONvalue="${org['GroupId']}"selected="selected">${org['GroupName']}</OPTION>
</c:if>
<c:iftest="${groupId!=org['GroupId']}">
<OPTIONvalue="${org['GroupId']}">${org['GroupName']}</OPTION>
</c:if>
</c:forEach>
</SELECT>
</TD>
</TR>
<TR>
<TDclass=zi_3F6293align="left"height="35"width="50">群&nbsp;&nbsp;&nbsp;&nbsp;组:</TD>
<td>

<SELECTstyle="width:150Px"name="columnInfoId"id="columnInfoId">
<OPTIONvalue="ALL">---全部---</OPTION>

</SELECT>
</TD>
</TR>


<tr>

</table>

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