JSONObject和JSONArray

需要注意的是网络传输时,需要用字符串进行传输,所以需要先将json对象用toString()转换为字符串形式,再在ajax里面用$.parseJSON转换成json对象。
JSONObject插入元素用put,JSONArray插入元素用add

$.ajax({	
	url:getBasePath()+"/ANoteServlet",
	async:true,
	cache:false,
	type:"post",
	contentType: "application/json; charset=utf-8",
	data:JSON.stringify(param1),
	success:function(msg){
		
		$("#noteUl li").css("display","none");
		var message = $.parseJSON(msg);//将json类型字符串转换为json对象				
		if(message.noteNum==0)
		{	
			$("#4notes").css("display","none");				
			$("#latestNote").append("<h5>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;暂无笔记&nbsp;&nbsp;您可以选择<a href='/fileManagement/personalPage/add-note.jsp'>创建笔记</a></h5>");
		}else 
		{
			for(i=0;i<message.noteNum;i++)
			{	
				$("#noteUl").children("li").eq(i).css("display","block");
				$("#noteUl").children("li").eq(i).find("a").html(message.noteList[i]);
				$("#noteUl").children("li").eq(i).children("div").eq(0).children("span").eq(1).html(message.courseNameOfNoteList[i]);
				$("#noteUl").children("li").eq(i).children("div").eq(1).html(message.createTimeOfNoteList[i]);  								
			}					
		}
	
	},
	error:function(response,status){		
	console.log(status);					
	}				
});	
CourseDao courseDao = new CourseDao();
 JSONArray jsonArray = new JSONArray();	
 JSONArray noteNumList = new JSONArray();
 JSONArray folderNumList = new JSONArray();
 List<Course> courseList = (List<Course>)courseDao.find2LatestCourse(currentUser.getUserId());
 for(int i = 0;i<courseList.size();i++){
	 jsonArray.add(courseList.get(i).getName());
	 noteNumList.add(courseList.get(i).getNoteNum());
	 folderNumList.add(courseList.get(i).getFolderNum());
	  
 }
 o.put("courseNum", courseList.size());
 o.put("courseList", jsonArray);
 o.put("noteNumList", noteNumList);
 o.put("folderNumList", folderNumList);
全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 12:04
毕业生招你惹你了,问一个发薪日来一句别看网上乱七八糟的你看哪个工作没有固定发薪日扭头就取消了面试就问了一句公司都是这个态度吗还搞上人身攻击了...
程序员小白条:呃呃呃,都还没面试,我都不会问这么细,何况通不通过,去不去都另说,你没实力和学历的话,在外面就这样,说实话没直接已读不回就不错了,浪费时间基本上
点赞 评论 收藏
分享
06-18 13:28
已编辑
门头沟学院 Web前端
爱睡觉的冰箱哥:《给予你300的工资》,阴的没边了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 13:47
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务