js怎么把字符串转化成jsonarray 如何将js中字符串转成json对象数组
js\u4e2d\u7684jsonarray\u5bf9\u8c61\u600e\u4e48\u8f6c\u6210\u5b57\u7b26\u4e32\u4f8b\u5982\uff1a
JSON\u5b57\u7b26\u4e32:
var str1 = '{ "name": "cxh", "sex": "man" }';
JSON\u5bf9\u8c61:
var str2 = { "name": "cxh", "sex": "man" };
\u4e00\u3001JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aJSON\u5bf9\u8c61
\u8981\u4f7f\u7528\u4e0a\u9762\u7684str1\uff0c\u5fc5\u987b\u4f7f\u7528\u4e0b\u9762\u7684\u65b9\u6cd5\u5148\u8f6c\u5316\u4e3aJSON\u5bf9\u8c61\uff1a
//\u7531JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aJSON\u5bf9\u8c61
var obj = eval('(' + str + ')');
\u6216\u8005
var obj = str.parseJSON(); //\u7531JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aJSON\u5bf9\u8c61
\u6216\u8005
var obj = JSON.parse(str); //\u7531JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aJSON\u5bf9\u8c61
\u7136\u540e\uff0c\u5c31\u53ef\u4ee5\u8fd9\u6837\u8bfb\u53d6\uff1a
Alert(obj.name);
Alert(obj.sex);
\u7279\u522b\u6ce8\u610f\uff1a\u5982\u679cobj\u672c\u6765\u5c31\u662f\u4e00\u4e2aJSON\u5bf9\u8c61\uff0c\u90a3\u4e48\u4f7f\u7528eval\uff08\uff09\u51fd\u6570\u8f6c\u6362\u540e\uff08\u54ea\u6015\u662f\u591a\u6b21\u8f6c\u6362\uff09\u8fd8\u662fJSON\u5bf9\u8c61\uff0c\u4f46\u662f\u4f7f\u7528parseJSON\uff08\uff09\u51fd\u6570\u5904\u7406\u540e\u4f1a\u6709\u95ee\u9898\uff08\u629b\u51fa\u8bed\u6cd5\u5f02\u5e38\uff09\u3002
\u4e8c\u3001\u53ef\u4ee5\u4f7f\u7528toJSONString()\u6216\u8005\u5168\u5c40\u65b9\u6cd5JSON.stringify()\u5c06JSON\u5bf9\u8c61\u8f6c\u5316\u4e3aJSON\u5b57\u7b26\u4e32\u3002
\u4f8b\u5982\uff1a
var last=obj.toJSONString(); //\u5c06JSON\u5bf9\u8c61\u8f6c\u5316\u4e3aJSON\u5b57\u7b26
\u6216\u8005
var last=JSON.stringify(obj); //\u5c06JSON\u5bf9\u8c61\u8f6c\u5316\u4e3aJSON\u5b57\u7b26
alert(last);
var obj = str.parseJSON(); //\u7531JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aJSON\u5bf9\u8c61
var str="{'name':'aaaa','age':34}";//json字符串var strobj = eval ("(" + str + ")");
绛旓細浣跨敤`JSON.parse`鏂规硶闈炲父绠鍗銆傚亣璁炬垜浠湁涓涓狫SON瀛楃涓诧紝鎴戜滑鍙渶瑕佸皢杩欎釜瀛楃涓蹭綔涓哄弬鏁颁紶閫掔粰`JSON.parse`鏂规硶鍗冲彲銆備緥濡傦細javascript let jsonString = '{"name": "John", "age": 30}';let jsonObject = JSON.parse;鍦ㄨ繖涓緥瀛愪腑锛宍jsonObject`鐜板湪鏄竴涓寘鍚玚name`鍜宍age`灞炴х殑JavaScript...
绛旓細涓銆丣SON瀛楃涓茶浆鎹负JSON瀵硅薄 瑕佷娇鐢ㄤ笂闈㈢殑str1锛屽繀椤讳娇鐢ㄤ笅闈㈢殑鏂规硶鍏堣浆鍖栦负JSON瀵硅薄锛//鐢盝SON瀛楃涓茶浆鎹负JSON瀵硅薄 var obj = eval('(' + str + ')');鎴栬 var obj = str.parseJSON(); //鐢盝SON瀛楃涓茶浆鎹负JSON瀵硅薄 鎴栬 var obj = JSON.parse(str); //鐢盝SON瀛楃涓茶浆鎹负JSON瀵硅薄 ...
绛旓細json瀛楃涓茶浆鎹㈡垚json鏁扮粍骞堕亶鍘嗗睘鎬у兼柟寮忎竴锛浣跨敤Iterator杩唬鍣ㄩ亶鍘嗗彇鍊 鏂规硶浜岋細浣跨敤for寰幆
绛旓細姣斿涓涓瀛楃涓var str = 'abc';锛屼綘鐢╯tr.split('')鐨勬柟娉曞氨浼氬皢瀹冨彉涓烘暟缁刐a,b,c]銆傛垨鑰呬綘鍙互鐩存帴鍙栦笅鏍囨瘮濡俿tr[0]寰楀埌'a'锛宻tr.length=3銆傝繕鏈涢噰绾宠阿璋
绛旓細//棣栧厛鎸夌収&鎶婃瘡涓琛岄兘鍒嗗紑鏉ar str = "1*鏃爏im鍗℃垨鍗℃棤鏁*宸茬粡杩炴帴*5*2014-01-10 09:23:46&1*鏃爏im鍗℃垨鍗℃棤鏁*宸茬粡杩炴帴*5*2014-01-10 09:23:50&1*鏃爏im鍗℃垨鍗℃棤鏁*宸茬粡杩炴帴*5*2014-01-10 09:23:54";var mapArr = str.split('&');//鍚ф瘡涓琛岄兘鍒嗗紑鏉apArray = mapArray....
绛旓細obj = {}; obj.text = arr[i]; obj.value = arr[i]; newArray.push(obj); } alert(newArray); 鎴栬 var str = '["鍚屾剰","涓嶅悓鎰"]'; var arr = JSON.parse(str); var newArray = []; for (var i = 0 ; i < arr....
绛旓細var str="{'name':'aaaa','age':34}";//json瀛楃涓 var strobj = eval ("(" + str + ")");
绛旓細閿欒鍘熷洜锛氬墠闈綘鑳芥垚鍔熷彇鍊兼槸鍥犱负a鏄竴涓猨avascript瀵硅薄鑰屼笉鏄函绮圭殑瀛楃涓锛屾墍浠ュ彲浠ョ敤obj.attr鐨勬柟寮忚鍙栨暟鍊硷紝鍚庨潰浣犵敤ajax鑾峰彇鐨勭浉浼兼牸寮忓瓧绗︿覆鐨勫疄闄涓篔SON鏍煎紡鐨勫瓧绗︿覆锛屾墍浠ョ敤瀵硅薄鏂规硶璇诲煎け璐ャ傝В鍐虫濊矾锛氬皢JSON鏂囨湰杞崲涓JavaScript 瀵硅薄鍚庡啀鐢╫bj.attr鐨勬柟寮忚鍊笺侸SON 鏂囨湰鏍煎紡鍦ㄨ娉曚笂涓庡垱寤 ...
绛旓細var str='{aa:1,bb:2,cc:3}';var json=eval("("+str+")");console.log(json);
绛旓細1锛歫s涓嬬敤eval鐢熸垚JSON瀵硅薄 ---閫氳繃eval() 鍑芥暟鍙互灏JSON瀛楃涓茶浆鍖栦负瀵硅薄銆2锛氫娇鐢ㄥ嚱鏁版柟寮 3锛氫娇鐢╦s鐨json搴撴垨鑰卝Query鎻愪緵鐨刯s搴 --- 濡傛灉鍩轰簬瀹夊叏鐨勮冭檻鐨勮瘽锛屾渶濂芥槸浣跨敤涓涓 JSON 瑙f瀽鍣ㄣ 涓涓 JSON 瑙f瀽鍣ㄥ皢鍙帴鍙 JSON 鏂囨湰銆傛墍浠ユ槸鏇村畨鍏ㄧ殑銆傜ず渚嬩唬鐮佸涓嬶細<!DOCTYPE HTML PUBLIC ...