本文實(shí)例講述了jQuery操作json常用方法。分享給大家供大家參考,具體如下:
在項(xiàng)目中經(jīng)常會(huì)用到獲取json中的某個(gè)值,或者動(dòng)態(tài)的創(chuàng)建一個(gè)json對(duì)象,今天簡(jiǎn)單的做了一個(gè)通用的js
/** * json工具 */var JsonUtil = (function(){ return { /** * 獲取json中的單個(gè)值 */ getValue:function(jsonObject,name){ var value = ""; $.each(jsonObject,function(n,v){ if(name == n){ value = v; return false; } }); return value; }, /** * 獲取json中的name 以數(shù)組形式返回 */ getNames:function(jsonObject){ var names = []; $.each(jsonObject,function(n,v){ names.push(n); }); return names; }, /** * 創(chuàng)建json對(duì)象 */ createJsonObject:function(){ this.jsonObectArr = []; } }})();/** * 創(chuàng)建json的nam和value的名值對(duì)字符串 */JsonUtil.createJsonObject.prototype.createJsonStr = function (name,value){ if (typeof value == 'string'){ this.jsonObectArr.push("/""+name+"/":"+"/""+value+"/""); }else{ this.jsonObectArr.push("/""+name+"/":"+value); }}/** * 獲取json對(duì)象 */JsonUtil.createJsonObject.prototype.getJson = function (){ var str = "{"+this.jsonObectArr.join(',')+"}"; return $.parseJSON(str);}
PS:關(guān)于json操作,這里再為大家推薦幾款比較實(shí)用的json在線工具供大家參考使用:
在線JSON代碼檢驗(yàn)、檢驗(yàn)、美化、格式化工具:
http://tools.VeVB.COm/code/json
JSON在線格式化工具:
http://tools.VeVB.COm/code/jsonformat
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.VeVB.COm/code/xmljson
json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.VeVB.COm/code/jsoncodeformat
在線json壓縮/轉(zhuǎn)義工具:
http://tools.VeVB.COm/code/json_yasuo_trans
C語(yǔ)言風(fēng)格/HTML/CSS/json代碼格式化美化工具:
http://tools.VeVB.COm/code/ccode_html_css_json
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery操作json數(shù)據(jù)技巧匯總》、《jQuery表單操作總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jquery中Ajax用法總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jQuery擴(kuò)展技巧總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注