Json概要
10457 ワード
もっと読む
1.Json導入
1.Json導入
JSON:JavaScript (JavaScriptObjectNotation)。
JSON 。 XML。
JSON XML 、 , 。
2.Json形式文法
JSON
{"name":" ","age":22}
JSON
{
"student":[
{"name":" ","age":22},
{"name":" ","age":23},
{"name":" ","age":24}
]
}
JSON
{
"student":[
{"name":" ","age":22,"score":{"chinese":90,"math":100,"english":80}},
{"name":" ","age":23,"score":{"chinese":70,"math":90,"english":90}},
{"name":" ","age":24,"score":{"chinese":80,"math":60,"english":90}}
]
}
Json Json
vardataObj = eval("("+data+")"); // json
3.Json第三者jarカバン導入
Json-lib
4.Ajax&Json相互作用
jar
commons-beanutils-1.7.0.jar
commons-collections-3.2.jar
commons-lang-2.4.jar
commons-logging-1.0.4.jar
ezmorph-1.0.3.jar
json-lib-2.2.3-jdk15.jar
GetAjaxInfoServlet.java
package com.andrew.web;
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 net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class GetAjaxInfoServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String action=request.getParameter("action");
if("jsonObject".equals(action)){
this.getJsonObject(request, response);
}else if("jsonArray".equals(action)){
this.getJsonArray(request, response);
}else if("jsonNested".equals(action)){
this.getJsonNested(request, response);
}
}
private void getJsonObject(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
// String resultJson="{\"name\":\" \",\"age\":22}";
JSONObject resultJson=new JSONObject();
resultJson.put("name", " ");
resultJson.put("age", 22);
out.println(resultJson);
out.flush();
out.close();
}
private void getJsonArray(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
JSONObject resultJson=new JSONObject();
JSONArray jsonArray=new JSONArray();
JSONObject jsonObject1=new JSONObject();
jsonObject1.put("name", " ");
jsonObject1.put("age", 22);
JSONObject jsonObject2=new JSONObject();
jsonObject2.put("name", " ");
jsonObject2.put("age", 23);
JSONObject jsonObject3=new JSONObject();
jsonObject3.put("name", " ");
jsonObject3.put("age", 24);
jsonArray.add(jsonObject1);
jsonArray.add(jsonObject2);
jsonArray.add(jsonObject3);
resultJson.put("students", jsonArray);
out.println(resultJson);
out.flush();
out.close();
}
private void getJsonNested(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
JSONObject resultJson=new JSONObject();
JSONArray jsonArray=new JSONArray();
JSONObject jsonObject1=new JSONObject();
jsonObject1.put("name", " ");
jsonObject1.put("age", 22);
JSONObject scoreObject1=new JSONObject();
scoreObject1.put("chinese", 90);
scoreObject1.put("math", 100);
scoreObject1.put("english", 80);
jsonObject1.put("score", scoreObject1);
JSONObject jsonObject2=new JSONObject();
jsonObject2.put("name", " ");
jsonObject2.put("age", 23);
JSONObject scoreObject2=new JSONObject();
scoreObject2.put("chinese", 70);
scoreObject2.put("math", 90);
scoreObject2.put("english", 90);
jsonObject2.put("score", scoreObject2);
JSONObject jsonObject3=new JSONObject();
jsonObject3.put("name", " ");
jsonObject3.put("age", 24);
JSONObject scoreObject3=new JSONObject();
scoreObject3.put("chinese", 80);
scoreObject3.put("math", 60);
scoreObject3.put("english", 90);
jsonObject3.put("score", scoreObject3);
jsonArray.add(jsonObject1);
jsonArray.add(jsonObject2);
jsonArray.add(jsonObject3);
resultJson.put("students", jsonArray);
out.println(resultJson);
out.flush();
out.close();
}
}
web.xml
getAjaxInfoServlet
com.andrew.web.GetAjaxInfoServlet
getAjaxInfoServlet
/getAjaxInfo
ajax.jsp
Insert title here
function loadInfo(){
var xmlHttp;
if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}else{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
alert(xmlHttp.responseText);
var dataObj=eval("("+xmlHttp.responseText+")");
alert(dataObj.name);
alert(dataObj.age);
document.getElementById("name").value=dataObj.name;
document.getElementById("age").value=dataObj.age;
}
};
xmlHttp.open("get", "getAjaxInfo?action=jsonObject", true);
xmlHttp.send();
}
function loadInfo2(){
var xmlHttp;
if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}else{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
alert(xmlHttp.responseText);
var dataObj=eval("("+xmlHttp.responseText+")");
var st=document.getElementById("studentTable");
alert(dataObj.students.length);
var newTr; //
var newTd0; //
var newTd1; //
var newTd2; //
for(var i=0;i<dataObj.students.length;i++){
var student=dataObj.students[i];
newTr=st.insertRow();
newTd0=newTr.insertCell();
newTd1=newTr.insertCell();
newTd2=newTr.insertCell();
newTd0.innerHTML=student.name;
newTd1.innerHTML=student.age;
newTd2.innerHTML=" :"+student.score.chinese+", :"+student.score.math+", :"+student.score.english;
}
}
};
// xmlHttp.open("get", "getAjaxInfo?action=jsonArray", true);
xmlHttp.open("get", "getAjaxInfo?action=jsonNested", true);
xmlHttp.send();
}
: :
http://localhost:8080/HeadFirstAjaxJsonChap03/ajax.jsp
Ajax
22
Ajax 2
22 :90, :100, :80
23 :70, :90, :90
24 :80, :60, :90