Ajax当中json的用法

马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。




例 3.1(eval1.html)

<HTML>
<HEAD>
<TITLE>在eclipse中直接open with火狐即可</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
//例1
var s = "2+41-19";
alert(eval(s));
eval("d =new Date();alert(d.toLocaleString())")

//-->
</SCRIPT>
</BODY>
</HTML>





例 3.2(eval2.html)


<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
var user ="{name:'某',sex:'男',age:22}";
var obj = eval("("+user+")"); //多加一对()为语法要求
alert(obj.age);
                 for(var pro in obj)
                 {
                     alert(obj[pro]);
                 }
//-->
</SCRIPT>
</BODY>
</HTML>



例 3.3(eval3.html)



<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>

<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
define = "{name:'Michael',email:'17bity@gmail.com'}";
 eval("data = "+define);
 alert(data.name);
 alert(data.email);

//-->
</SCRIPT>
</BODY>
</HTML>




例 3.4


<%@ page language="java" contentType="text/html; charset=gb2312"
   %>
<html>
<head>
<title>Insert title here</title>
</head>
<script>
function show(t)
{
    var xmlhttp;
    try{
            xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
        }catch(e){
            try{
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }catch(e){
                            try{
                                xmlhttp=new XMLHttpRequest();
                                }catch(e){}
                            }
                }
    xmlhttp.open("get","servlet34?id="+t);
    xmlhttp.onreadystatechange=function(){
        if(xmlhttp.readyState==4)
        {
            if(xmlhttp.status==200)
            {
                str = xmlhttp.responseText;
/*about the following part, refer to eval2.html*/
                 obj= eval("("+str+")");

                 for(var pro in obj)
                 {
                     document.getElementById(pro).innerHTML=obj[pro];
                 }


            }
        }
    }
    xmlhttp.send(null);
}
</script>
<body>

<table width="340" border="1">
  <tr>
     <td id="1" onclick="show(this.id)">点这1</td>
     <td id="2" onclick="show(this.id)">点这2</td>
</tr>
</table>

<table width="340" border="1">
      <tr>
        <td width="170">参数项</td>
        <td width="170">参数值</td>
      </tr>
      <tr>
        <td>型号</td>
        <td id="type">&nbsp;</td>
      </tr>
      <tr>
        <td>price</td>
        <td  id="price">&nbsp;</td>
      </tr>
    </table>
</body>
</html>



package helloWorld;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class servlet34 extends HttpServlet {
       protected void doPost(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
        }
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/html;charset=gb2312");
            PrintWriter out = response.getWriter();
            String id = request.getParameter("id");
            System.out.println(id);
            StringBuffer sb = new StringBuffer();
            sb.append("{");
            sb.append("type:'");
            sb.append("adsl马克-to-win:" + id + "");
            sb.append("',");
            sb.append("price:'" + "333mark" + id + "'");
            sb.append("}");
            System.out.print("sb.toString() is" + sb.toString());
            out.print(sb.toString());
        }
}






例 3.5




<html>
<head>
<title>Insert title here</title>
</head>
<script>
    function show(t) {
        var xmlhttp;
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                try {
                    xmlhttp = new XMLHttpRequest();
                } catch (e) {
                }
            }
        }
        xmlhttp.open("get", "servlet35?id=" + t);
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) {
                    str = xmlhttp.responseText;
             
                    arr = eval("(" + str + ")");

                    for ( var i = 0; i < arr.length; i++) {
                        var obj = arr[i];
                        for ( var pro in obj) {
                             document.getElementById(pro).innerHTML = obj[pro];
                        }
                    }

                }
            }
        }
        xmlhttp.send(null);

    }
</script>

<body>
    <table width="340" border="1">
        <tr>
            <td id="1" onclick="show(this.id)">点这1</td>
            <td id="2" onclick="show(this.id)">点这2</td>
        </tr>
    </table>

 <table width="340" border="1">
        <tr>
            <td width="170">参数项</td>
            <td width="170">参数值</td>
        </tr>
        <tr>
            <td>型号</td>
            <td id="type">&nbsp;</td>
        </tr>
        <tr>
            <td>price1</td>
            <td id="price">&nbsp;</td>
        </tr>
        <tr>
            <td>型号1</td>
            <td id="type1">&nbsp;</td>
        </tr>
        <tr>
            <td>price</td>
            <td id="price1">&nbsp;</td>
        </tr>
    </table>
</body>
</html>




package helloWorld;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class servlet35 extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = response.getWriter();
        String id = request.getParameter("id");
        System.out.println(id);
        JSONArray arr = new JSONArray();
        JSONObject o = new JSONObject ();

                o.put("type","adsl马克-to-win:"+id);
                o.put("price","333马克-to-win:"+id);
                arr.add(o);
                o = new JSONObject ();
                o.put("type1","adsl马克-to-win:"+id+id);
                o.put("price1","666马克-to-win:"+id+id);
                arr.add(o);
   
        System.out.println("arr.toString() is"+ arr.toString());
        out.print(arr.toString());
    }
}






例 3.6




<html>
<head>
<title>Insert title here</title>
</head>
<script>
    function show(t) {
        var xmlhttp;
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                try {
                    xmlhttp = new XMLHttpRequest();
                } catch (e) {
                }
            }
        }
        xmlhttp.open("get", "servlet36?id=" + t);
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) {
                    var xmldoc = xmlhttp.responseXML;
                    document.getElementById("type").innerHTML = xmldoc
                            .getElementsByTagName("type")[0].firstChild.nodeValue;
                    document.getElementById("price").innerHTML = xmldoc
                            .getElementsByTagName("price")[0].firstChild.nodeValue;
                }
            }
        }
        xmlhttp.send(null);
    }
</script>

<body>
    <table width="340" border="1">
        <tr>
            <td id="1" onclick="show(this.id)">点这1</td>
            <td id="2" onclick="show(this.id)">点这2</td>
        </tr>
    </table>

    <table width="340" border="1">
        <tr>
            <td width="170">参数项q</td>
            <td width="170">参数值</td>
        </tr>
        <tr>
            <td>型号</td>
            <td id="type">&nbsp;</td>
        </tr>
        <tr>
            <td>price</td>
            <td id="price">&nbsp;</td>
        </tr>
    </table>
</body>
</html>




package helloWorld;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class servlet36 extends HttpServlet {
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        /*
         * when you write back a XML stream, you must write as
         * text/xml;charset=UTF-8
         */
        response.setContentType("text/xml;charset=UTF-8");
        // response.setContentType("text/xml");
        PrintWriter out = response.getWriter();
        String id = request.getParameter("id");
        /* the following first statement can be ommited, */
      //  out.print("<?xml version=\"1.0\" ?>");
      
        out.print("<root>");
        out.print("<type>" + "adsl马克-to-win:" + id + "</type>");
        out.print("<price>" + "333马克-to-win:" + id + "</price>");
        out.print("</root>");
     //   System.out.print("<?xml version=\"1.0\" ?>");
        System.out.print("<root>");
        System.out.print("<type>" + "adsl马克-to-win:" + id + "</type>");
        System.out.print("<price>" + "333马克-to-win:" + id + "</price>");
        System.out.print("</root>");
    }
}