日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

服务器和前台采用JSON通讯

發(fā)布時(shí)間:2025/3/17 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 服务器和前台采用JSON通讯 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.


封裝實(shí)體類(lèi)(服務(wù)器返回信息都存放在該實(shí)體中)

package?com.chinaseacom.store.common;public?class?ResponseBody?{public?static?final?int?CODE_SUCCESS=1;public?static?final?int?CODE_FAIL=0;private?int?code=1;???//0:failtrue;?1:successprivate?String?message;?//private?String?errors;private?String?accessToken;private?Object?result;public?ResponseBody(int?code,?String?message,?String?errors,?Object?result,?String?accessToken)?{super();this.code?=?code;this.message?=?message;this.errors?=?errors;this.result?=?result;this.accessToken?=?accessToken;}public?String?getAccessToken()?{return?accessToken;}public?void?setAccessToken(String?accessToken)?{this.accessToken?=?accessToken;}/***?@return?the?code*/public?int?getCode()?{return?code;}/***?@param?code?the?code?to?set*/public?void?setCode(int?code)?{this.code?=?code;}/***?@return?the?message*/public?String?getMessage()?{return?message;}/***?@param?message?the?message?to?set*/public?void?setMessage(String?message)?{this.message?=?message;}/***?@return?the?errors*/public?String?getErrors()?{return?errors;}/***?@param?errors?the?errors?to?set*/public?void?setErrors(String?errors)?{this.errors?=?errors;}/***?@return?the?result*/public?Object?getResult()?{return?result;}/***?@param?result?the?result?to?set*/public?void?setResult(Object?result)?{this.result?=?result;}}


服務(wù)器返回json字符串

??protected?void?writeResponseByJsonStr(Object?javabean,int?code,?String?message,?String?error){try?{HttpServletResponse?response=?getResponse();response.setContentType("application/json;?charset=utf-8");response.setHeader("Cache-Control",?"no-cache");?//取消瀏覽器緩存PrintWriter?out?=?response.getWriter();out.print(JSON.toJSONString(new?ResponseBody(code,message,error,javabean,accessToken)??));//實(shí)體對(duì)象轉(zhuǎn)換為json字符串傳遞out.flush();out.close();}?catch?(IOException?e)?{e.printStackTrace();}}


前臺(tái)解析json字符串(采用fastjson)步驟

import?java.io.BufferedReader; import?java.io.IOException; import?java.io.InputStreamReader; import?java.net.URL; import?java.util.List;import?com.alibaba.fastjson.JSON; import?com.alibaba.fastjson.JSONArray; import?com.alibaba.fastjson.JSONObject;import?com.chinaseacom.store.customer.model.Notice;public?class?MobileClient?{public?static?void?main(String[]?args)?throws?IOException?{BufferedReader?reader?=?null;try?{URL?url?=?new?URL("http://localhost:8080/customer/notice/list");reader?=?new?BufferedReader(new?InputStreamReader(url.openStream()));String?jsonString?=?reader.readLine();System.out.println(jsonString);JSONObject?jsonObject?=?JSON.parseObject(jsonString);?//?得到j(luò)son對(duì)象JSONArray?jsonArray?=?(JSONArray)?jsonObject.get("result");//?根據(jù)key得到j(luò)son數(shù)組System.out.println(jsonArray.toJSONString());List<Notice>?nlist?=?JSON.parseArray(jsonArray.toJSONString(),Notice.class);?//?json數(shù)組轉(zhuǎn)換成對(duì)象集合for?(Notice?notice?:?nlist)?{System.out.println(notice.getUpdateDate());}}?catch?(Exception?e)?{e.printStackTrace();}?finally?{reader.close();}}}



參考文章:使用FastJSON,將對(duì)象或數(shù)組和JSON串互轉(zhuǎn)

http://blog.csdn.net/gaojinshan/article/details/30260707


本文出自 “點(diǎn)滴積累” 博客,請(qǐng)務(wù)必保留此出處http://tianxingzhe.blog.51cto.com/3390077/1704002

總結(jié)

以上是生活随笔為你收集整理的服务器和前台采用JSON通讯的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。