JavaScript Json http://www.dreamdu.com/blog/2008/10/19/json_in_javascript/
JSON :http://www.json.org/json-zh.html
<script type=" text/javascript " src="js/json2.js "></script> <script type = "text/javascript"> var xmlHttp; /*创建XMLHttpRequest对象 兼容Firefox,Ie浏览器*/ function createXMLHttpRequest(){ if(window.ActiveXObject){ xmlHttp = new ActiveXObject( " Microsoft.XMLHTTP " ); } else if(window.XMLHttpRequest){ xmlHttp = new XMLHttpRequest(); } } /*发出Ajax请求并调用处理函数*/ function doJSON() { var car = getCarObject(); // Use the JSON JavaScript library to stringify the Car object var carAsJSON = JSON.stringify(car); alert( " Car object as JSON:\n " + carAsJSON + " type "+typeof carAsJSON); var url = "JSONExample.json"; createXMLHttpRequest(); xmlHttp.open("POST" , url, true); xmlHttp.onreadystatechange = handleStateChange; xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xmlHttp.send(carAsJSON); } /*判断XMLHttpRequest对象的返回状态*/ function handleStateChange() { if (xmlHttp.readyState == 4 ) { if (xmlHttp.status == 200 ) { parseResults(); } } } /*处理从服务器返回的结果*/ function parseResults() { var result = xmlHttp.responseText; alert(result +" before JSON.parse(String) type "+typeof result); var resultObj = JSON.parse(result); alert(resultObj.name +" "+resultObj.height+" after JSON.parse(String) type "+typeof resultObj); var responseDiv = document.getElementById("serverResponse"); if (responseDiv.hasChildNodes()) { responseDiv.removeChild(responseDiv.childNodes[ 0 ]); } var responseText = document.createTextNode(xmlHttp.responseText); responseDiv.appendChild(responseText); } /*获得一个Car对象*/ function getCarObject(){ return new Car("Dodge","Coronet R/T",1968,"yellow"); } /*Car对象*/ function Car(make, model, year, color) { this.make = make; this.model = model; this.year = year; this.color = color; } </script> <br/><br/> <form action="#"> <input type="button" value="Click here to send JSON data to the server" onclick="doJSON();"/> </form> <h2>Server Response:</h2> <div id="serverResponse"></div> package com.nstars; import java.io.BufferedReader; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONException; import org.json.JSONObject; public class JSONExample extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } /*响应Post请求*/ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String json = readJSONStringFromRequestBody(request); // Use the JSON-Java binding library to create a JSON object in Java JSONObject jsonObject = null; try { jsonObject = new JSONObject(json); } catch (Exception e) { e.printStackTrace(); } String responseText = ""; try { responseText = " You have a " + jsonObject.getInt("year") + " " + jsonObject.getString("make") + " " + jsonObject.getString("model") + " " + " that is " + jsonObject.getString("color") + " in color. "; } catch (JSONException e) { e.printStackTrace(); } JSONObject jsonObj = new JSONObject(); try { jsonObj.putOnce("name", "边苏涛"); jsonObj.put("height", 176); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } response.setContentType("text/xml"); response.setCharacterEncoding("UTF-8"); response.getWriter().print(jsonObj); } /*从request对象中读取JSON对象字符串*/ private String readJSONStringFromRequestBody(HttpServletRequest request) { StringBuffer json = new StringBuffer(); String line = null; try { BufferedReader reader = request.getReader(); while ((line = reader.readLine()) != null) { json.append(line); } } catch (Exception e) { System.out.println(" Error reading JSON string: " + e.toString()); } return json.toString(); } }