DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> json原理分析及實例介紹
json原理分析及實例介紹
編輯:關於JavaScript     
這次在項目中前後台的數據交互中用到了json,經過這段時間的使用,大概了解了一下,簡單總結一下json。
JSON:JavaScript 對象表示法(JavaScript Object Notation)。
JSON 是存儲和交換文本信息的語法。類似 XML。
JSON 比 XML 更小、更快,更易解析。
和 XML 一樣,JSON 也是基於純文本的數據格式。由於 JSON 天生是為 JavaScript 准備的,因此,JSON 的數據格式非常簡單,您可以用 JSON 傳輸一個簡單的 String,Number,Boolean,也可以傳輸一個數組,或者一個復雜的 Object 對象。
先看controller中的一段代碼。看主要是看從數據庫查詢出來的數據是怎樣以json的格式輸出的。
[java]
復制代碼 代碼如下:
@RequestMapping("/work/plan/checkSubmitForApproval")
public void checkSubmitForApproval(String planId,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
String result="{\"result\":\"faild\",\"personSituation\":\"null\"}";
HttpSession session = request.getSession();
String industryID = (String) session.getAttribute("industryID");
IIndustry industry = industryService.getById(industryID);
if(industry.getType().equals("XXX")){
try {
boolean flag = false;
IProjectMain yearPlan = projectPlanService.findProjectPlanById(planId);
List<IStaffInfo> listStaffInfo = sysStaffService.getStaffByPlanId(planId, industryID);
for(int i=0;i<listStaffInfo.size();i++){
if(listStaffInfo.get(i).getPractitionersPost().equals(StaffRole.PROGECTMANAGER.toString())){
flag = true;
}
}
if(flag == true){
result="{\"result\":\"success\",\"personSituation\":\""+yearPlan.getPerson_Situation()+"\"}";
}else{
result="{\"result\":\"success\",\"personSituation\":\""+yearPlan.getPerson_Situation()+"\",\"isManager\":\"false\"}";
}
} catch (Exception e) {
result="{\"result\":\"falid\"}";
throw new PlatformException(e);
}finally{
OutputUtils.write(response,result,"text/x-json;charset=UTF-8");
}

先PutputUtils中的write代碼
[java]
復制代碼 代碼如下:
public static void write(HttpServletResponse response, String text, String contentType)
{
PrintWriter out=null;
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType(contentType);
try
{
out = response.getWriter();
out.write(text);
}
catch (IOException e)
{
Logger.getLogger(OutputUtils.class).error(e.getMessage(), e);
} finally{
if(out!=null){
out.flush();
out.close();
}
}
}

其中的思路是得到response的printwriter,將要輸出的信息設置到其中。在界面層利用jquery的Post判斷返回的信息。
[javascript]
復制代碼 代碼如下:
<span style="white-space:pre"> </span>function distribute(){
var dplanId = $(".currli").attr("id");
if(dplanId != ""){
$.ajax({
type : "POST",
url :做驗證的action url,
dataType : "json",
success : function(data) {
//HAVE為已分配狀態
if (data.result == "success" && data.personSituation == "UNHAVE") {
with (document.getElementById("planForm")) {
action=驗證合法後要提交的url;
method="post";
submit();
}
<span style="white-space:pre"> </span>}

其中success:function(data)是一個回調函數,即上面做的驗證action的方法成功之後執行的操作。在jquery的使用方法詳情點擊這裡查看。
關於jquery的post提交不理解的同學,點擊這裡學習。
關於ajax和jquery的歷史,建議參見維基百科中,寫的很清楚。
jquery已經封裝好了從response中取data的操作,所以這裡用起來非常方便,省去了從xml中一點一點讀取的頭疼,給開發帶來了極大方便。
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved