jsonä½ä¸ºä¸ç§è½»é级
çæ°æ®äº¤æ¢æ ¼å¼ï¼å¨ååå°æ°æ®äº¤æ¢ä¸å æ®çé常éè¦çå°ä½ãJsonçè¯æ³é常ç®åï¼éç¨çæ¯é®å¼å¯¹è¡¨ç¤ºå½¢å¼ãJSON å¯ä»¥å° JavaScript
对象ä¸è¡¨ç¤ºçä¸ç»æ°æ®è½¬æ¢ä¸ºå符串ï¼ç¶åå°±å¯ä»¥å¨å½æ°ä¹é´è½»æ¾å°ä¼ éè¿ä¸ªå符串ï¼æè
å¨å¼æ¥åºç¨ç¨åºä¸å°åç¬¦ä¸²ä» Web
客æ·æºä¼ éç»æå¡å¨ç«¯ç¨åºï¼ä¹å¯ä»¥ä»æå¡å¨ç«¯ç¨åºä¼ éjsonæ ¼å¼çå符串ç»å端并ç±å端解éãè¿ä¸ªå符串æ¯ç¬¦åjsonè¯æ³çï¼èjsonè¯æ³åæ¯
javascriptè¯æ³çåéï¼æ以javascriptå¾å®¹æ解éå®ï¼èä¸
JSON å¯ä»¥è¡¨ç¤ºæ¯"å称 / å¼å¯¹"æ´å¤æçç»æãä¸é¢æ们éè¿å®ä¾æ¥ççJQueryä¼ é/解æjsonæ ¼å¼çæ°æ®æ¯å¦ä½å®ç°çã
1.é¦å
æ¥çå端jsp代ç ï¼
[html] view plain copy
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="/springMVC6/js/jquery-1.7.2.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
$(document).ready(function(){
//ä¼ éåç¬¦ä¸²æ ¼å¼json对象å°åå°ï¼ä¸ä¸ªjson对象ï¼
$("#resolveJsonObject").click(function(){
var userName =encodeURI($("#userName").attr("value"));
var age = encodeURI($("#age").attr("value"));
var user = {userName:userName,age:age};
var aMenu = encodeURI(JSON.stringify(user));
$.ajax({
url:"/springMVC6/user/data/resolveJsonObject" ,
data:"orderJson=" + aMenu,
success:function(data){
}
});
});
//ä¼ éjsonæ°ç»å°åå°
$("#resolveJsonArray").click(function(){
var userName =encodeURI($("#userName").attr("value"));
var age = encodeURI($("#age").attr("value"));
//æ°ç»å¼å§
var user1 = {userName:userName,age:age};
var allMenu={
"menu":[ ]
};
allMenu.menu.push(user1);
var allMenu1 = encodeURI(JSON.stringify(allMenu));
$.ajax({
//jsonæ°ç»
url:"/springMVC6/user/data/resolveJsonArray" ,
data:"orderJson=" + allMenu1,
success:function(data){
}
});
});
//æ¥æ¶åå°çjsonå¨åå°è§£æ
$("#resolveJson").click(function(){
$.ajax({
//解æä»åå°è¿åçjsonæ°æ®
url:"/springMVC6/user/data/resolveJson" ,
type:"post",
success:function(data){
var arr=eval(data);
alert(arr.length);
for(var m = 0;m<arr.length;m++){
alert(arr[m].user.userName);
}
}
});
});
});
</script>
</head>
<body>
<h1>jsonæ·»å ç¨æ·</h1>
å§åï¼<input id="userName" type="text" name="userName">
å¹´é¾:<input id="age" type="text" name="age"><br>
<input type="button" id="resolveJsonObject" value="json对象">
<input type="button" id="resolveJsonArray" value="jsonæ°ç»">
<input type="button" id="resolveJson" value="å端解æjsonå符串">
</body>
</html>
2.使ç¨javabean解æå端æ°æ®ï¼
[java] view plain copy
package com.tgb.web.controller.annotation;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.tgb.web.controller.entity.User;
@Controller
@RequestMapping("/user/data")
public class DataController {
//æ¥æ¶åå°ä¼ è¿æ¥çåç¬¦ä¸²æ ¼å¼çjson对象ï¼å¨åå°è¿è¡è§£æ
@RequestMapping("/resolveJsonObject" )
public void resolveJsonObject(HttpServletRequest request,HttpServletResponse response) throws IOException {
//解ç
String str = URLDecoder.decode(request.getParameter("orderJson"),"UTF-8");
JSONObject jb=new JSONObject();
//å°jsonæ ¼å¼çå符串转æ¢ä¸ºjson对象ï¼å¹¶åå¾è¯¥å¯¹è±¡çâuserNameâå±æ§å¼
String o=(String)jb.fromObject(str).get("userName");
System.out.println(o);
}
//ä¼ éjsonæ°ç»å符串
@RequestMapping("/resolveJsonArray" )
public void resolveJsonArray(HttpServletRequest request,HttpServletResponse response) throws IOException {
//解ç ï¼ä¸ºäºè§£å³ä¸æä¹±ç
String str = URLDecoder.decode(request.getParameter("orderJson"),"UTF-8");
JSONObject jb=new JSONObject();
//å°jsonæ ¼å¼çå符串转æ¢ä¸ºjsonæ°ç»å¯¹è±¡
JSONArray array=(JSONArray)jb.fromObject(str).get("menu");
//åå¾jsonæ°ç»ä¸ç第ä¸ä¸ªå¯¹è±¡
JSONObject o = (JSONObject) array.get(0);//è·å¾ç¬¬ä¸ä¸ªarrayç»æ
//ååºjsonæ°ç»ä¸ç¬¬ä¸ä¸ªå¯¹è±¡çâuserNameâå±æ§å¼
String name=o.get("userName").toString();//è·å¾å±æ§å¼
System.out.println(name);
}
//éè¿è¯¥å½æ°è¿åjsonæ ¼å¼çæ°æ®ï¼å¨åå°éè¿JQueryè¿è¡è§£æ
@RequestMapping("/resolveJson" )
public void resolveJson(HttpServletRequest request,HttpServletResponse response) throws IOException {
List m = (List) new ArrayList();
JSONArray jsons = new JSONArray();
for(int i=0;i<10;i++){
User user = new User();
user.setUserName("name_" + i);
m.add(user);
}
for(int j=0;j<m.size();j++){
JSONObject jsonObject = new JSONObject();
jsonObject.put("user", m.get(j));
jsons.add(jsonObject);
}
response.getWriter().print(jsons.toString()) ;
}
@RequestMapping("/toJson" )
public String toJson() {
return "/json";
}
}
jsonçä½ç¨å¹¶ä¸ä»
ä»
å¨äºä½ä¸ºå符串å¨ååå°è¿è¡ä¼ éï¼æ们éç¨jsonä¼ éæ°æ®çæ¶åæ´ä¸»è¦çèèå°çæ¯å®çä¼ è¾æçãå½ä¸¤ä¸ªç³»ç»éè¦è¿è¡æ°æ®äº¤æ¢çæ¶åï¼å¦æä¼ éçæ¯ç»è¿
åºååç对象ï¼æçæ¯é常ä½çï¼å¦æä¼ éçæ¯åå¨å¤§é对象çæ°ç»çæ¶åæçå°±æ´ä¸æ¢æ³è±¡äºï¼è¿æ¶å¦æéè¿å°å¯¹è±¡ææ°æ®è½¬æ¢æjsonå符串è¿è¡ä¼ éï¼æç
å°±ä¼æé«å¾å¤ã