Ext JSON(JavaScript Object Notation)對(duì)象的字符串是一種輕量級(jí)的數(shù)據(jù)交換格式,常見于前端和后端之間的數(shù)據(jù)傳遞。通過(guò)使用JSON格式,可以將JavaScript對(duì)象轉(zhuǎn)換為字符串,并在需要時(shí)反轉(zhuǎn)換回來(lái)。
{ "name": "Tom", "age": 25, "interests": ["music", "sports", "reading"], "address": { "city": "Shanghai", "district": "Pudong New Area", "street": "Century Avenue" } }
以上是一個(gè)JSON對(duì)象的字符串表示。它由一個(gè)對(duì)象組成,對(duì)象的每個(gè)屬性都由名稱和值組成。其中,name和age是基本數(shù)據(jù)類型,interests是一個(gè)數(shù)組類型,address是一個(gè)嵌套對(duì)象類型。
在ExtJS中,可以使用Ext.JSON.encode()和Ext.JSON.decode()方法來(lái)進(jìn)行JSON對(duì)象和字符串之間的轉(zhuǎn)換。例如,以下代碼將一個(gè)JavaScript對(duì)象轉(zhuǎn)換為字符串:
var obj = { name: "Lucy", age: 20, interests: ["dancing", "traveling"], address: { city: "Beijing", district: "Chaoyang District", street: "Gongti North Road" } }; var jsonString = Ext.JSON.encode(obj); console.log(jsonString);
輸出結(jié)果為:
{"name":"Lucy","age":20,"interests":["dancing","traveling"],"address":{"city":"Beijing","district":"Chaoyang District","street":"Gongti North Road"}}
同樣地,以下代碼將一個(gè)JSON字符串轉(zhuǎn)換為JavaScript對(duì)象:
var jsonString = '{"name":"Jack","age":30,"interests":["photography","cooking"],"address":{"city":"Guangzhou","district":"Tianhe District","street":"Tianhe Road"}}'; var obj = Ext.JSON.decode(jsonString); console.log(obj);
輸出結(jié)果為:
{ "name": "Jack", "age": 30, "interests": ["photography", "cooking"], "address": { "city": "Guangzhou", "district": "Tianhe District", "street": "Tianhe Road" } }
總結(jié):Ext JSON對(duì)象的字符串是一種常見的數(shù)據(jù)交換格式,方便實(shí)現(xiàn)不同系統(tǒng)之間的數(shù)據(jù)傳遞。在ExtJS中,可以使用Ext.JSON.encode()和Ext.JSON.decode()方法來(lái)進(jìn)行JSON對(duì)象和字符串之間的轉(zhuǎn)換。