Jquery json jsp实例

3 篇文章 0 订阅
3 篇文章 0 订阅

自己总结的json 各个细节,方便自己回顾

package com.cpic.servlet;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.*;
import java.util.*;

/**
 * json对象
 * 1.json包6个  json.js和jquery.js
 * 2.resp.setContentType("appliction/json;charset=utf-8"); 
 *   resp.setContentType("text/javascript;charset=utf-8"); 
 *   dataType: "json",
 * 3.后台传过去的数据格式一定要对!
 * 4.接受解析的方法要对!
 * @author Administrator
 *
 */

public class JsonServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		req.setCharacterEncoding("utf-8");
		resp.setContentType("appliction/json;charset=utf-8"); 
		//获得前台数据
		String sss = (String)req.getParameter("json");
		PrintWriter out =resp.getWriter();
		
		//------------------------------------------------
		String name="zhangsan";
		String age ="22";
		//单个JSON对象:单个数据和多个数据(直接写入参数和变量两种) 请仔细查看格式!
		//正确
        //out.print(JSONObject.fromObject("{test:'hehe',t2:'haha'}").toString());
        //out.print(JSONObject.fromObject("{name:'"+name+"',age:'"+age+"'}").toString());
		
		//错误没有引号
        //out.print(JSONObject.fromObject("{test:hehe,t2:haha}").toString());
        //out.print(JSONObject.fromObject("{name:"+name+","age:"+age+"}").toString());
		
		//----------------------------------------------
		
		//JSON数据对象:可以用数组 或者 用List
		
		//数组
	   //String[] str = GetFirstFloorValue();
	   //System.out.println(JSONArray.fromObject(str).toString());
       //out.print(JSONArray.fromObject(str).toString());	       
	    
		
	   //List
	   //List<String> list =getList();        
	   //System.out.println(JSONArray.fromObject(list).toString());
       //out.print(JSONArray.fromObject(list).toString());


       
        out.flush();
        out.close();
	    }

	    private String[] GetFirstFloorValue() {
	        String[] str = new String[4];
	        String t="java1";
         
	        //错误的!
//	        str[0] = "{test:"+t+",t2:"+t+"}";
//	        str[1] = "{test:"+t+",t2:"+t+"}";
//	        str[2] = "{test:"+t+",t2:"+t+"}";
//	        str[3] = "{test:"+t+",t2:"+t+"}";
//	        str[0] = "{test:桌面应用,t2:桌面应用}";
//	        str[1] = "{test:移动设备,t2:移动设备}";
//	        str[2] = "{test:服务器端,t2:服务器端}";
//	        str[3] = "{test:嵌入式开发,t2:嵌入式开发}";
	        //正确的! 一定要有引号!
//	        str[0] = "{test:'"+t+"',test2:'"+t+"'}";
//	        str[1] = "{test:'"+t+"',test2:'"+t+"'}";
//	        str[2] = "{test:'"+t+"',test2:'"+t+"'}";
//	        str[3] = "{test:'"+t+"',test2:'"+t+"'}";
	        str[0] = "{test:'桌面应用',test2:'桌面应用'}";
	        str[1] = "{test:'移动设备',test2:'移动设备'}";
	        str[2] = "{test:'服务器端',test2:'服务器端'}";
	        str[3] = "{test:'嵌入式开发',test2:'嵌入式开发'}";
	        
	        return str;
	    }

		public List<String> getList(){
			String name ="java2";
            String age="22";
            
            String a[]=new String[2];
            a[0]="{name:'"+name+"',age:'"+age+"'}";
            a[1]="{name:'"+name+"',age:'"+age+"'}";
            
            List<String> list= new ArrayList<String>();
            list.add(a[0]);
            list.add(a[1]);
			return list;
		}


}


 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<script src="./Js/jquery-1.6.4.js" language="javascript" ></script>
<script src="./Js/json.js" language="javascript"></script>
<script language="javascript">
	function ads(){
		$.ajax({
			url: "json.do",
			type: "post",
			data: {json:$("#input").val(),test:$("#input").val()},
			dataType: "json",
			success: function(msg){
			
			//JSONObject
			//alert(msg.name+"|"+msg.age);
			
			
			//JSONArray 数组
			//$.each(msg,function(i,item){
			//	alert(item.test+"|"+item.test2);
			//})			
			
			
	
			//JSONArray List
			//$.each(msg,function(i,item){
			//	alert(item.name+"|"+item.age);
			//})
			}
		})
	}
	
</script>

  <input type="text" id="input" />
  <input type="button" value="确定" οnclick="ads()"/>
  <table id="tb">
  
  </table>
</html>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值