json对象和josn字符串有什么区别 JSON对象和Json字符串的区别
Json\u5bf9\u8c61\u548cJson\u5b57\u7b26\u4e32\u7684\u533a\u522bJSON\u5b57\u7b26\u4e32\u4e0eJSON\u5bf9\u8c61\u7684\u533a\u522b
Q:\u4ec0\u4e48\u662f"JSON\u5b57\u7b26\u4e32"\uff0c\u4ec0\u4e48\u662f"JSON\u5bf9\u8c61"\uff0c\u4e24\u8005\u7684\u533a\u522b?
a.JSON\u5bf9\u8c61\u662f\u76f4\u63a5\u53ef\u4ee5\u4f7f\u7528JQuery\u64cd\u4f5c\u7684\u683c\u5f0f\uff0c\u5982C#\u4e2d\u53ef\u4ee5\u7528\u5bf9\u8c61\uff08\u7c7b\u540d\uff09\u70b9\u51fa\u5c5e\u6027\uff08\u65b9\u6cd5\uff09\u4e00\u6837b.JSON\u5b57\u7b26\u4e32\u4ec5\u4ec5\u53ea\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4e00\u4e2a\u6574\u4f53\uff0c\u4e0d\u622a\u53d6\u7684\u8bdd\u6ca1\u529e\u6cd5\u53d6\u51fa\u5176\u4e2d\u5b58\u50a8\u7684\u6570\u636e\uff0c\u4e0d\u80fd\u76f4\u63a5\u4f7f\u7528\uff0c\u9664\u975e\u4f60\u53ea\u60f3alert()\u4ed6\uff1b
JSON\u5bf9\u8c61:
var str2 = { "name": "deluyi", "sex": "man" };
JSON\u5b57\u7b26\u4e32:
var str1 = '{ "name": "deyuyi", "sex": "man" }';
Q:\u5c06"JSON\u5b57\u7b26\u4e32"\u8f6c\u5316\u4e3a"JSON\u5bf9\u8c61"\u7684\u65b9\u6cd5?
\u4e00:\u4f7f\u7528$.parseJSON(str)
//\u7531JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aJSON\u5bf9\u8c61var str='{ "name": "John" }';var obj = jQuery.parseJSON(str)alert("1"+ obj.name);
ps:\u6b64\u79cd\u65b9\u5f0f\u4ec5\u652f\u6301\u6807\u51c6\u683c\u5f0f\uff1avar str='{ "name": "John" }';\u4e8c:JSON.parse(str)
var str = '{ "name": "mady", "age": "24" }';var obj = JSON.parse(str);alert(obj.name);
ps:\u6b64\u79cd\u65b9\u5f0f\u4ec5\u652f\u6301\u6807\u51c6\u683c\u5f0f\uff1avar str='{ "name": "John" }';
\u4e09:\u4f7f\u7528eval('('+str+')');
//\u7531JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aJSON\u5bf9\u8c61var str='{ "name": "John" }';var obj = eval('(' + str + ')');alert( obj.name);var str2="{ 'name': 'John' }";var obj2 = eval('(' + str2 + ')');alert( obj2.name);var str3="{ name: 'John' }";var obj3 = eval('(' + str3 + ')');alert( obj3.name);
\u4ee5\u4e0a\u5747\u4f1a\u8f93\u51fa\u7ed3\u679c\u201cjohn\u201d\u3002
Eval\u65b9\u5f0f\u53ef\u4ee5\u8f6c\u6362\u4ee5\u4e0b\u6807\u51c6\u548c\u975e\u6807\u51c6\u683c\u5f0f\u5b57\u7b26\u4e32\uff1a
var str="{ 'name': 'John' }";var str2='{ "name": "John" }';var str3="{ name: 'John' }";
Q:\u5c06"JSON\u5bf9\u8c61"\u8f6c\u5316\u4e3a"JSON\u5b57\u7b26\u4e32"\u7684\u65b9\u6cd5?
\u4e00:\u4f7f\u7528\u5168\u5c40\u65b9\u6cd5JSON.stringify()\u4e0etoJSONString()
\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);
Q:\u8bfb\u53d6JSON\u5bf9\u8c61\u7684\u65b9\u6cd5?
\u5982\u4e0a\u9762\u7684\u4f8b\u5b50\uff1a
var str2 = { "name": "mady", "sex": "man" };
\u5c31\u53ef\u4ee5\u8fd9\u6837\u8bfb\u53d6\uff1a
alert(str2.name);//\u548cC#\u4e00\u6837\u76f4\u63a5\u5f80\u51fa\u70b9\u2026
\u5f39\u51fa\u201d mady\u201d\u3002
\u6211\u4eec\u9047\u5230\u7684JSON\u5f88\u5c11\u6709\u8fd9\u4e48\u7b80\u5355\u7684\uff0c\u6bd4\u5982\u590d\u6742\u4e00\u70b9\u7684JSON\u5bf9\u8c61\u5982\uff1a
var str={"GetUserPostByIdResult":{"Age":"33","ID":"2server","Name":"mady"}};
\u89e3\u6790\u7528\uff1a
alert(str.GetUserPostByIdResult.Name);//\u4e00\u6b21\u70b9\u4e0d\u51fa\u6765\uff0c\u6211\u591a\u70b9\u51e0\u6b21
\u5f39\u51fa\uff1a\u201dmady\u201d\u3002
JSON\u5b57\u7b26\u4e32\u4e0eJSON\u5bf9\u8c61\u7684\u533a\u522b
Q:\u4ec0\u4e48\u662f"JSON\u5b57\u7b26\u4e32"\uff0c\u4ec0\u4e48\u662f"JSON\u5bf9\u8c61"\uff0c\u4e24\u8005\u7684\u533a\u522b?
a.JSON\u5bf9\u8c61\u662f\u76f4\u63a5\u53ef\u4ee5\u4f7f\u7528JQuery\u64cd\u4f5c\u7684\u683c\u5f0f\uff0c\u5982C#\u4e2d\u53ef\u4ee5\u7528\u5bf9\u8c61\uff08\u7c7b\u540d\uff09\u70b9\u51fa\u5c5e\u6027\uff08\u65b9\u6cd5\uff09\u4e00\u6837b.JSON\u5b57\u7b26\u4e32\u4ec5\u4ec5\u53ea\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4e00\u4e2a\u6574\u4f53\uff0c\u4e0d\u622a\u53d6\u7684\u8bdd\u6ca1\u529e\u6cd5\u53d6\u51fa\u5176\u4e2d\u5b58\u50a8\u7684\u6570\u636e\uff0c\u4e0d\u80fd\u76f4\u63a5\u4f7f\u7528\uff0c\u9664\u975e\u4f60\u53ea\u60f3alert()\u4ed6\uff1b
JSON\u5bf9\u8c61:
var str2 = { "name": "deluyi", "sex": "man" };
JSON\u5b57\u7b26\u4e32:
var str1 = '{ "name": "deyuyi", "sex": "man" }';
Q:\u5c06"JSON\u5b57\u7b26\u4e32"\u8f6c\u5316\u4e3a"JSON\u5bf9\u8c61"\u7684\u65b9\u6cd5?
\u4e00:\u4f7f\u7528$.parseJSON(str)
//\u7531JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aJSON\u5bf9\u8c61var str='{ "name": "John" }';var obj = jQuery.parseJSON(str)alert("1"+ obj.name);
ps:\u6b64\u79cd\u65b9\u5f0f\u4ec5\u652f\u6301\u6807\u51c6\u683c\u5f0f\uff1avar str='{ "name": "John" }';\u4e8c:JSON.parse(str)
var str = '{ "name": "mady", "age": "24" }';var obj = JSON.parse(str);alert(obj.name);
ps:\u6b64\u79cd\u65b9\u5f0f\u4ec5\u652f\u6301\u6807\u51c6\u683c\u5f0f\uff1avar str='{ "name": "John" }';
\u4e09:\u4f7f\u7528eval('('+str+')');
//\u7531JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aJSON\u5bf9\u8c61var str='{ "name": "John" }';var obj = eval('(' + str + ')');alert( obj.name);var str2="{ 'name': 'John' }";var obj2 = eval('(' + str2 + ')');alert( obj2.name);var str3="{ name: 'John' }";var obj3 = eval('(' + str3 + ')');alert( obj3.name);
\u4ee5\u4e0a\u5747\u4f1a\u8f93\u51fa\u7ed3\u679c\u201cjohn\u201d\u3002
Eval\u65b9\u5f0f\u53ef\u4ee5\u8f6c\u6362\u4ee5\u4e0b\u6807\u51c6\u548c\u975e\u6807\u51c6\u683c\u5f0f\u5b57\u7b26\u4e32\uff1a
var str="{ 'name': 'John' }";var str2='{ "name": "John" }';var str3="{ name: 'John' }";
Q:\u5c06"JSON\u5bf9\u8c61"\u8f6c\u5316\u4e3a"JSON\u5b57\u7b26\u4e32"\u7684\u65b9\u6cd5?
\u4e00:\u4f7f\u7528\u5168\u5c40\u65b9\u6cd5JSON.stringify()\u4e0etoJSONString()
\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);
Q:\u8bfb\u53d6JSON\u5bf9\u8c61\u7684\u65b9\u6cd5?
\u5982\u4e0a\u9762\u7684\u4f8b\u5b50\uff1a
var str2 = { "name": "mady", "sex": "man" };
\u5c31\u53ef\u4ee5\u8fd9\u6837\u8bfb\u53d6\uff1a
alert(str2.name);//\u548cC#\u4e00\u6837\u76f4\u63a5\u5f80\u51fa\u70b9\u2026
\u5f39\u51fa\u201d mady\u201d\u3002
\u6211\u4eec\u9047\u5230\u7684JSON\u5f88\u5c11\u6709\u8fd9\u4e48\u7b80\u5355\u7684\uff0c\u6bd4\u5982\u590d\u6742\u4e00\u70b9\u7684JSON\u5bf9\u8c61\u5982\uff1a
var str={"GetUserPostByIdResult":{"Age":"33","ID":"2server","Name":"mady"}};
\u89e3\u6790\u7528\uff1a
alert(str.GetUserPostByIdResult.Name);//\u4e00\u6b21\u70b9\u4e0d\u51fa\u6765\uff0c\u6211\u591a\u70b9\u51e0\u6b21
\u5f39\u51fa\uff1a\u201dmady\u201d\u3002
JSON字符串与JSON对象的区别
Q:什么是"JSON字符串",什么是"JSON对象",两者的区别?
a.JSON对象是直接可以使用JQuery操作的格式,如C#中可以用对象(类名)点出属性(方法)一样b.JSON字符串仅仅只是一个字符串,一个整体,不截取的话没办法取出其中存储的数据,不能直接使用,除非你只想alert()他;
JSON对象:
var str2 = { "name": "deluyi", "sex": "man" };
JSON字符串:
var str1 = '{ "name": "deyuyi", "sex": "man" }';
Q:将"JSON字符串"转化为"JSON对象"的方法?
一:使用$.parseJSON(str)
//由JSON字符串转换为JSON对象var str='{ "name": "John" }';var obj = jQuery.parseJSON(str)alert("1"+ obj.name);
ps:此种方式仅支持标准格式:var str='{ "name": "John" }';二:JSON.parse(str)
var str = '{ "name": "mady", "age": "24" }';var obj = JSON.parse(str);alert(obj.name);
ps:此种方式仅支持标准格式:var str='{ "name": "John" }';
三:使用eval('('+str+')');
//由JSON字符串转换为JSON对象var str='{ "name": "John" }';var obj = eval('(' + str + ')');alert( obj.name);var str2="{ 'name': 'John' }";var obj2 = eval('(' + str2 + ')');alert( obj2.name);var str3="{ name: 'John' }";var obj3 = eval('(' + str3 + ')');alert( obj3.name);
以上均会输出结果“john”。
Eval方式可以转换以下标准和非标准格式字符串:
var str="{ 'name': 'John' }";var str2='{ "name": "John" }';var str3="{ name: 'John' }";
Q:将"JSON对象"转化为"JSON字符串"的方法?
一:使用全局方法JSON.stringify()与toJSONString()
例如:
var last=obj.toJSONString(); //将JSON对象转化为JSON字符
或者
var last=JSON.stringify(obj); //将JSON对象转化为JSON字符
alert(last);
Q:读取JSON对象的方法?
如上面的例子:
var str2 = { "name": "mady", "sex": "man" };
就可以这样读取:
alert(str2.name);//和C#一样直接往出点…
弹出” mady”。
我们遇到的JSON很少有这么简单的,比如复杂一点的JSON对象如:
var str={"GetUserPostByIdResult":{"Age":"33","ID":"2server","Name":"mady"}};
解析用:
alert(str.GetUserPostByIdResult.Name);//一次点不出来,我多点几次
弹出:”mady”。
绛旓細7銆json鏄竴绉嶆暟鎹被鍨嬶紝姣斿json1 = a锛宐锛宑a锛宐锛宑鏄悓涓绉嶇被鍨嬬殑锛屽彲浠ユ槸瀵硅薄锛屼篃鍙互鏄瀛楃涓鍜屾暟瀛椼8銆乵eta琛ㄧず鍏冩暟鎹紝灏辨槸涓浜涙渶鍩烘湰鐨勪俊鎭紝json鏄竴绉嶆暟鎹粨鏋勶紝鍦╦s涓箍娉涘簲鐢ㄣ9銆乯son 鏄痡son鏂囦欢锛宩son绠鍗曡灏辨槸javascript涓殑瀵硅薄鍜鏁扮粍锛屾墍浠ヨ繖涓ょ缁撴瀯灏辨槸瀵硅薄鍜屾暟缁勪袱绉嶇粨鏋勶紝...
绛旓細灏哖ython鏁版嵁杞崲涓篔SON鏍煎紡锛屽彲浠ヤ娇鐢╜json.dumps()`鍑芥暟锛屽叾涓璥ensure_ascii=False`鍙傛暟鍏佽姝g‘鏄剧ず涓枃銆傜浉鍙嶏紝濡傛灉闇瑕佸皢JSON瀛楃涓杞崲鍥濸ython鏁版嵁锛屽彲浠ヤ娇鐢╜json.loads()`鍑芥暟銆傝繖绉嶈浆鎹㈢殑渚垮埄鎬у湪浜庯紝Python鐨勫瓧鍏稿拰鍒楄〃鍙互鐩存帴鏄犲皠鍒JSON瀵硅薄鍜鏁扮粍锛屾棤闇棰濆鐨勭紪鐮佸拰瑙g爜姝ラ銆侸SON鐨勬祦琛屽湪浜庡叾璺...
绛旓細2銆乯son涔熷彲鐢ㄤ簬鍓嶅悗绔箣闂翠簰鐩镐紶閫json鏁版嵁銆傛瘮濡傚墠绔彂璧疯姹傦紝璋冪敤鎺ュ彛锛屽悗绔繑鍥炰竴涓瞛son鏁版嵁,澶勭悊鏁版嵁锛屾覆鏌撳埌椤甸潰涓娿3銆佺敱浜JSON闈炲父绠鍗曪紝寰堝揩灏遍闈eb涓栫晫锛屽苟涓旀垚涓篍CMA鏍囧噯銆4銆佸嚑涔庢墍鏈夌紪绋嬭瑷閮芥湁瑙f瀽JSON鐨勫簱锛屽鏋滄垜浠敹鍒颁竴涓狫SON鏍煎紡鐨瀛楃涓锛屽彧闇瑕佹妸瀹冨弽搴忓垪鍖栨垚涓涓狫avaScript瀵硅薄锛屽氨...
绛旓細int杞崲鎴恓son瀵硅薄褰撶劧鏄┖鐨勪簡锛json瀵硅薄鍜宩son瀛楃涓涓嶄竴鏍凤紝json瀵硅薄瑕佹湁閿煎锛坘ey,value锛
绛旓細鏍¢獙宸ュ叿锛1銆佸帇缂╄浆涔 绋嬪簭鍛樺湪鍐橨SON璇彞娴嬭瘯鐢ㄤ緥鐨勬椂鍊欙紝寰堝鏃跺欎负浜嗘柟渚跨洿鎺ュ啓浜嗕釜JSON瀛楃涓鍋氭祴璇曪紝浣嗘槸鍙堥櫡鍏ヤ簡鏃犳澧冪殑鍙屽紩鍙疯浆涔夌殑楹荤儲涓傝繖娆惧姛鑳介泦鍘嬬缉銆佽浆涔変簬涓韬紝璁╀綘鍦ㄥ啓娴嬭瘯鐢ㄤ緥鐨勬椂鍊欏楸煎緱姘淬2銆佸湪绾垮彂閫JSON鏁版嵁 澶у閮界煡閬擄紝JSON鐢ㄧ殑鏈澶氱殑杩樻槸WEB椤圭洰鐨勫紑鍙戯紝閭d綘瑕佹祴璇曚竴涓...
绛旓細鍦↗avaScript涓紝JSON瀵硅薄涓寘鍚簡涓や釜鏂规硶锛歱arse()鏂规硶銆乻tringify()鏂规硶锛涗娇鐢↗SON瀵硅薄鐨勮繖涓や釜鏂规硶鍙互瀹炵幇JSON瀛楃涓鍜孞avaScript瀵硅薄鐨勪簰鐩歌浆鎹傛帴涓嬫潵鐨勮繖绡囨枃绔犲氨鏉ョ粰澶у浠嬬粛鍏充簬JavaScript涓璊SON瀵硅薄鐨勪娇鐢ㄦ柟娉曘侸avaScript涓璊SON鏄粈涔堬紵JSON鏄竴绉嶆槸鐢ㄤ簬瀛樺偍鍜屼紶杈撴暟鎹殑鏍煎紡锛涗竴涓交閲忕骇鐨勪汉绫诲彲璇荤殑...
绛旓細JavaScript 瀛楃涓鍜孞SON 瀛楃涓鐨勬渶澶у尯鍒湪浜庯紝JSON 瀛楃涓插繀椤讳娇鐢 鍙屽紩鍙 銆傚厛鏉ョ湅涓涓爣鍑嗙殑 JavaScript 瀵硅薄瀛楅潰閲忥細浣 JSON 琛ㄧず瀵硅薄濡備笅鎵绀猴細JSON 瑕佹眰蹇呴』缁欏璞$殑灞炴у姞寮曞彿銆傚疄闄呬笂锛屽湪 JavaScript 涓紝瀵硅薄瀛楅潰閲忓畬鍏ㄥ彲浠ュ啓鎴愪笅闈 杩欓噷鍙互鐪嬪嚭锛JSON瀵硅薄鏈涓や釜涓嶄竴鏍风殑鍦版柟锛氬睘鎬х殑鍊...
绛旓細json瀵硅薄銆傚彲浠ョ偣鍑哄璞¢噷闈㈢殑灞炴с傘傘
绛旓細杩欎釜瀛楃涓鐪嬭捣鏉ユ湁鐐瑰効鍙ゆ紝浣嗘槸JavaScript寰堝鏄撹В閲婂畠锛岃屼笖 JSON 鍙互琛ㄧず姣"鍚嶇О / 鍊煎"鏇村鏉傜殑缁撴瀯銆備緥濡傦紝鍙互琛ㄧず鏁扮粍鍜屽鏉傜殑瀵硅薄锛岃屼笉浠呬粎鏄敭鍜屽肩殑绠鍗曞垪琛ㄣ傚悕绉 / 鍊煎 鎸夌収鏈绠鍗曠殑褰㈠紡锛屽彲浠ョ敤涓嬮潰杩欐牱鐨 JSON 琛ㄧず"鍚嶇О / 鍊煎"锛歿"firstName":"Brett"} 杩欎釜绀轰緥闈炲父鍩烘湰锛...
绛旓細json搴忓垪鍖栧悗鏈熬鍑虹幇闂彿鐨勫師鍥犳湁锛1銆侀潪娉曞瓧绗︽垨缂栫爜闂锛欽SON瑙勮寖瑕佹眰鎵鏈夌殑瀛楃涓插煎繀椤讳娇鐢ㄥ弻寮曞彿鎷捣鏉ワ紝鑰屼笉鏄崟寮曞彿锛屽湪JSON瀛楃涓蹭腑浣跨敤浜嗗崟寮曞彿锛屽垯搴忓垪鍖栬繃绋嬩腑浼氬嚭鐜扮紪鐮侀棶棰橈紝瀵艰嚧瀛楃涓插簭鍒楀寲鍚庢湯灏惧嚭鐜伴棶鍙枫2銆佺紪鐮佹牸寮忎笉鍖归厤锛欽SON搴忓垪鍖栬繃绋嬩腑锛屽皢Python瀵硅薄杞寲涓篔SON瀛楃涓鏃讹紝闇瑕佺‘淇...