生活随笔
收集整理的這篇文章主要介紹了
JSON javascript 使用
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
http://www.iteye.com/topic/71343
JSON (JavaScript Object Notation)一種簡(jiǎn)單的數(shù)據(jù)格式,比xml更輕巧。 JSON 是 JavaScript 原生格式,這意味著在 JavaScript 中處理 JSON 數(shù)據(jù)不需要任何特殊的 API 或工具包。
JSON的規(guī)則很簡(jiǎn)單: 對(duì)象是一個(gè)無(wú)序的“‘名稱/值’對(duì)”集合。一個(gè)對(duì)象以“{”(左括號(hào))開(kāi)始,“}”(右括號(hào))結(jié)束。每個(gè)“名稱”后跟一個(gè)“:”(冒號(hào));“‘名稱/值’ 對(duì)”之間使用“,”(逗號(hào))分隔。具體細(xì)節(jié)參考http://www.json.org/json-zh.html
舉個(gè)簡(jiǎn)單的例子:
js 代碼
function?showJSON()?{ ??????var?user?=? ??????{? ??????????"username":"andy", ??????????"age":20, ??????????"info":?{?"tel":?"123456",?"cellphone":?"98765"}, ??????????"address": ??????????????[ ??????????????????{"city":"beijing","postcode":"222333"}, ??????????????????{"city":"newyork","postcode":"555666"} ??????????????] ??????} ?????? ??????alert(user.username); ??????alert(user.age); ??????alert(user.info.cellphone); ??????alert(user.address[0].city); ??????alert(user.address[0].postcode); ??} ?? 這表示一個(gè)user對(duì)象,擁有username, age, info, address 等屬性。
同樣也可以用JSON來(lái)簡(jiǎn)單的修改數(shù)據(jù),修改上面的例子
js 代碼
function?showJSON()?{ ??????var?user?=? ??????{? ??????????"username":"andy", ??????????"age":20, ??????????"info":?{?"tel":?"123456",?"cellphone":?"98765"}, ??????????"address": ??????????????[ ??????????????????{"city":"beijing","postcode":"222333"}, ??????????????????{"city":"newyork","postcode":"555666"} ??????????????] ??????} ?????? ??????alert(user.username); ??????alert(user.age); ??????alert(user.info.cellphone); ??????alert(user.address[0].city); ??????alert(user.address[0].postcode); ?????? ??????user.username?=?"Tom"; ??????alert(user.username); ??} ?? ?JSON提供了json.js包,下載http://www.json.org/json.js 后,將其引入然后就可以簡(jiǎn)單的使用object.toJSONString()轉(zhuǎn)換成JSON數(shù)據(jù)。
js 代碼
function?showCar()?{ ??????var?carr?=?new?Car("Dodge",?"Coronet?R/T",?1968,?"yellow"); ??????alert(carr.toJSONString()); ??} ????function?Car(make,?model,?year,?color)???????{ ???????this.make??=??make; ???????this.model??=??model; ???????this.year??=??year; ???????this.color??=??color; ??} ?? 可以使用eval來(lái)轉(zhuǎn)換JSON字符到Object
js 代碼
function?myEval()?{ ??????var?str?=?'{?"name":?"Violet",?"occupation":?"character"?}'; ??????var?obj?=?eval('('?+?str?+?')'); ??????alert(obj.toJSONString()); ??} ?? 或者使用parseJSON()方法
js 代碼
function?myEval()?{ ??????var?str?=?'{?"name":?"Violet",?"occupation":?"character"?}'; ??????var?obj?=?str.parseJSON(); ??????alert(obj.toJSONString()); ??} ?? 下面使用prototype寫一個(gè)JSON的ajax例子。
先寫一個(gè)servlet (我的是servlet.ajax.JSONTest1.java)就寫一句話 java 代碼
response.getWriter().print("{?\"name\":?\"Violet\",?\"occupation\":?\"character\"?}");?? 再在頁(yè)面中寫一個(gè)ajax的請(qǐng)求
js 代碼
function?sendRequest()?{ ??????var?url?=?"/MyWebApp/JSONTest1"; ??????var?mailAjax?=?new?Ajax.Request( ??????????url, ??????????{ ??????????????method:?'get', ??????????????onComplete:?jsonResponse ??????????} ??????); ??} ????function?jsonResponse(originalRequest)?{ ??????alert(originalRequest.responseText); ??????var?myobj?=?originalRequest.responseText.parseJSON(); ??????alert(myobj.name); ??} ?? prototype-<st1:chsdate w:st="on" isrocdate="False" islunardate="False" day="30" month="12" year="1899">1.5.1</st1:chsdate>.js中提供了JSON的方法,String.evalJSON(), 可以不使用json.js, 修改上面的方法
js 代碼
function?jsonResponse(originalRequest)?{ ??????alert(originalRequest.responseText); ??????var?myobj?=?originalRequest.responseText.evalJSON(true); ??????alert(myobj.name); ??} ?? JSON還提供了java的jar包 http://www.json.org/java/index.html API也很簡(jiǎn)單,下面舉個(gè)例子
在javascript中填加請(qǐng)求參數(shù)
js 代碼
function?sendRequest()?{ ??????var?carr?=?new?Car("Dodge",?"Coronet?R/T",?1968,?"yellow"); ??????var?pars?=?"car="?+?carr.toJSONString(); ????????var?url?=?"/MyWebApp/JSONTest1"; ??????var?mailAjax?=?new?Ajax.Request( ??????????url, ??????????{ ??????????????method:?'get', ??????????????parameters:?pars, ??????????????onComplete:?jsonResponse ??????????} ??????); ??} ?? 使用JSON請(qǐng)求字符串就可以簡(jiǎn)單的生成JSONObject并進(jìn)行解析,修改servlet添加JSON的處理(要使用json.jar)
java 代碼
private?void?doService(HttpServletRequest?request,?HttpServletResponse?response)?throws?IOException?{ ??????????String?s3?=?request.getParameter("car"); ??????????try?{ ??????????????JSONObject?jsonObj?=?new?JSONObject(s3); ??????????????System.out.println(jsonObj.getString("model")); ??????????????System.out.println(jsonObj.getInt("year")); ??????????}?catch?(JSONException?e)?{ ??????????????e.printStackTrace(); ??????????} ??????????response.getWriter().print("{?\"name\":?\"Violet\",?\"occupation\":?\"character\"?}"); ??????} ?? 同樣可以使用JSONObject生成JSON字符串,修改servlet
java 代碼
private?void?doService(HttpServletRequest?request,?HttpServletResponse?response)?throws?IOException?{ ??????????String?s3?=?request.getParameter("car"); ??????????try?{ ??????????????JSONObject?jsonObj?=?new?JSONObject(s3); ??????????????System.out.println(jsonObj.getString("model")); ??????????????System.out.println(jsonObj.getInt("year")); ??????????}?catch?(JSONException?e)?{ ??????????????e.printStackTrace(); ??????????} ?????????? ??????????JSONObject?resultJSON?=?new?JSONObject(); ??????????try?{ ??????????????resultJSON.append("name",?"Violet") ????????????????????????.append("occupation",?"developer") ????????????????????????.append("age",?new?Integer(22)); ??????????????System.out.println(resultJSON.toString()); ??????????}?catch?(JSONException?e)?{ ??????????????e.printStackTrace(); ??????????} ??????????response.getWriter().print(resultJSON.toString()); ??????} ?? js 代碼
function?jsonResponse(originalRequest)?{ ??????alert(originalRequest.responseText); ??????var?myobj?=?originalRequest.responseText.evalJSON(true); ??????alert(myobj.name); ??????alert(myobj.age); ??}?? 參考
http://www.json.org/js.html
http://www.blogjava.net/Jkallen/archive/2006/03/28/37905.html
http://www.json.org/
http://www.prototypejs.org/learn/json
http://www.json.org/java/index.html
http://www.ibm.com/developerworks/cn/web/wa-ajaxintro10/index.html
?
總結(jié)
以上是生活随笔為你收集整理的JSON javascript 使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。