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

JSON小示例
JSON(JAVASCRIPT OBJECT Notation,JAVASCRIPT的对象表示法).
1.ajaxjson.html代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Ajax-JSON</title>
     <script type="text/javascript" src="jslib/jquery-1.3.1.js"></script>
    <script type="text/javascript" src="jslib/json.js"></script>
</head>
<body>
        <input type='button' value='Show JSON' onclick='getJSON();'/>
       <div id="show" style="disply:none;"></div>
</body>
</html>

json.js代码
function getJSON(){
    var url ="AjaxJSONServlet?dumb"+new Date();
    $.get(url, null, function (data){
        var obj = eval(data);
        var resultObj = $("#show");
        var message ='';
        for(var id in obj){
            var s = obj[id];
            message+=('ID:'+s.id+' NAME:'+s.name+' COUNTRY:'+s.country+' AGE:'+s.age);
           message +="<br/>";

        }
        resultObj.html(message);
    });
}

java服务器端代码(AjaxJSONServlet.java):
package com.ajax.test.part1;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2011-6-26
 * Time: 11:01:30
 * To change this template use File | Settings | File Templates.
 */
public class AjaxJSONServlet extends HttpServlet {

      @Override
    protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
       this.doPost(httpServletRequest, httpServletResponse);

    }

    @Override
    protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
      httpServletResponse.setContentType("text/html;charset=utf-8");
        PrintWriter out = httpServletResponse.getWriter();
/**返回一个数组(其中包含2个对象)***/
           StringBuffer buff = new StringBuffer();
        buff.append("[{");
        buff.append(" id:").append("'").append("01234").append("',");
        buff.append("name:").append("'").append("lyle").append("',");
        buff.append("age:").append("20").append(",");
               buff.append("country:").append("'").append("USA").append("'");
         buff.append("},{");
           buff.append(" id:").append("'").append("0000002").append("',");
        buff.append("name:").append("'").append("lyle du").append("',");
        buff.append("age:").append("29").append(",");
               buff.append("country:").append("'").append("CN").append("'");
        buff.append("}]");
          //System.print();
          out.print(buff);
        }

}

注意:在做该功能时,必须引入jquery.
现在有专门的jar包支持json,见附件json.jar.
使用json.jar小示例:
JSONObject jsonObject = new JSONObject();
jsonObject.put("a", 1);   jsonObject.put("b", 1.1);
jsonObject.put("c", 1L);
jsonObject.put("d", "test");
jsonObject.put("e", true);
System.out.println(jsonObject);
//{"d":"test","e":true,"b":1.1,"c":1,"a":1}  


public static String toJSONObj(Map<Object, Object>... jsonList) {
        StringBuffer buff = new StringBuffer();
        for (Map<Object, Object> jsonMap : jsonList) {
            Set<Entry<Object, Object>> set = jsonMap.entrySet();
            List<String> l = new ArrayList<String>();
            for (Iterator<Entry<Object, Object>> iter = set.iterator(); iter.hasNext();) {
                Entry<Object, Object> entry = iter.next();
                if (entry.getValue() instanceof Integer) {