1例如:
List-->json:
List<Student>list=newArrayList<Student>();
for(inti=0;i<stus.length;i++){
list.add(i);
}
JSONArrayjsonArray=JSONArray.fromObject(stus);
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:
Studentstudent=newStudent();
student.setAge(18);
student.setName("zhangsan");
JSONObjectjsonObject=JSONObject.fromObject(student);
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
Student[]stus=newStudent[5];
List<Student>stuList=newArrayList<Student>();
for(inti=0;i<stus.length;i++){
stus[i]=newStudent();
stus[i].setAge(20);
stus[i].setName("張三"+i);
stuList.add(stus[i]);
}
JSONObjectjsonObjectFromMap=JSONObject.fromObject(stuList);
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">機 構:</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">羣 組:</TD>
<td>
<SELECTstyle="width:150Px"name="columnInfoId"id="columnInfoId">
<OPTIONvalue="ALL">---全部---</OPTION>
</SELECT>
</TD>
</TR>
<tr>
</table>