日期:2014-05-16  浏览次数:20398 次

jQuery each()方法解析json数据
自己做的简单的jquery解析json的Demo
1.JsonServelet
package com.zhiming.jqueryajax;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

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

import net.sf.json.JSONObject;

public class JsonServlet extends HttpServlet
{

	private Stock szzs;
	private Stock pfyh;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		resp.setContentType("text/html");
		resp.setCharacterEncoding("utf-8");
		PrintWriter out = resp.getWriter();
		Map<String,Stock> maps=new HashMap<String,Stock>();
		maps.put(szzs.getName(), szzs);
		maps.put(pfyh.getName(), pfyh);
		JSONObject jsonObj=JSONObject.fromObject(maps);
		out.println(jsonObj);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		doGet(req, resp);
	}

	@Override
	public void init() throws ServletException
	{
        szzs=new Stock(3200.0,3500,1800,"上证综合",1);
        pfyh=new Stock(256.0,3850,1950,"浦发",2);
	}

}

2.一个对象类
package com.zhiming.jqueryajax;

public class Stock
{
	private double today;
	private double yestoday;
	private double Price;
	private String name;
	private Integer id;

	public Stock(double today, double yestoday, double price, String name,
			Integer id) {
		this.today = today;
		this.yestoday = yestoday;
		this.Price = price;
		this.name = name;
		this.id = id;
	}

	public double getToday()
	{
		return today;
	}

	public void setToday(double today)
	{
		this.today = today;
	}

	public double getYestoday()
	{
		return yestoday;
	}

	public void setYestoday(double yestoday)
	{
		this.yestoday = yestoday;
	}

	public double getPrice()
	{
		return Price;
	}

	public void setPrice(double price)
	{
		Price = price;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public Integer getId()
	{
		return id;
	}

	public void setId(Integer id)
	{
		this.id = id;
	}

	public String toString()
	{
		return "name:" + this.name + ",price" + this.Price;
	}

}

3.ajaxJson.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'ajaxJson.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
    <script type="text/javascript" src="web/js/jquery-1.4.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//要解析的json字符串如下
//{"浦发":{"id":2,"name":"浦发","price":1950,"today":256,"yestoday":3850},"上证综合":{"id":1,"name":"上证综合","price":1800,"today":3200,"yestoday":3500}} 
$("#button").click(function(){
//Ajax请求
/** 3、遍历对象(没有附加参数) 
  * $.each(Object, function(name, value) { 
  *     this;     //this指向当前属性的值 
  *     name;     //name表示Object当前属性的名称 
  *     value;    //value表示Object当前属性的值 
  * }); 
  */
$.get("JsonServlet",null,function(json){
//第一个$.each对json字符串进行解析会得到多个 name-object对象 name代表对象的名称
//第二个$.each对object对象进行迭代,可以得到对象的key和value
 $.each(json,function(name,object){  
 $.each(object,function(key,value){  
   alert("objectkey:"+name+" key:"+key+" value:"+value);  
       });  
 });
},"json")
});
})
</script&g